当前位置: 动力学知识库 > 问答 > 编程问答 >

node.js - google-auth-library: Refresh token not replaying request

问题描述:

While using Google's google-auth-library Node Module, my refresh token is not replaying requests correctly.

The message I receive in the err callback is:

Error: Invalid Credentials

I've seen other questions dealing with this issue, but the solutions presented in there did not solve my issue.

Details:

In the below code, assume the argument googleAccessToken to look like:

{

"access_token": "AN ACCESS TOKEN",

"refresh_token": "A REFRESH TOKEN"

}

A minimal version of my client:

exports.nextEvent = function (googleAccessToken, cb) {

// Load client secrets from a local file.

fs.readFile('client_secret.json', function processClientSecrets(err, clientSecretContent) {

if (err) {

console.error('Error loading client secret file: ' + err);

return;

}

authorize(JSON.parse(clientSecretContent), getNextEvent);

});

/**

* Create an OAuth2 client with the given credentials, and then execute the

* given callback function.

*

* @param {Object} credentials The authorization client credentials.

* @param {function} callback The callback to call with the authorized client.

*/

function authorize(clientCredentials, callback) {

var clientSecret = clientCredentials.web.client_secret;

var clientId = clientCredentials.web.client_id;

var redirectUrl = clientCredentials.web.redirect_uris[0];

var auth = new googleAuth();

var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);

oauth2Client.setCredentials(googleAccessToken);

callback(oauth2Client);

}

/**

*

*

* @param {google.auth.OAuth2} auth An authorized OAuth2 client.

*/

function getNextEvent(auth) {

var calendar = google.calendar('v3');

calendar.events.list({

auth: auth,

calendarId: 'primary',

timeMin: (new Date()).toISOString(),

maxResults: 10,

singleEvents: true,

orderBy: 'startTime'

},

function (err, response) {

if(err){

console.error(err)

}

console.log("your next events are : " + response);

}

);

}

};

网友答案:

Not sure if this is your exact use case, but I am using passport-google-oauth2 to get the initial access and refresh tokens.

Then, I use googleapis for the API requests.

oauth2Client.setCredentials({
  access_token: 'ACCESS TOKEN HERE',
  refresh_token: 'REFRESH TOKEN HERE'
});
plus.people.get({ userId: 'me', auth: oauth2Client }, function(err, response) {
  // handle err and response 
});

The documentation mentions that when the access token is expired, oauth2Client will automatically renew it using the refresh token and then replay the request.

Well, that part didn't work for me and I would always get Invalid Credentials.

Try setting the expiry_date to a unix timestamp more than an hour in the past.

oauth2Client.setCredentials({
  access_token: 'ACCESS TOKEN HERE',
  refresh_token: 'REFRESH TOKEN HERE'
  expiry_date: '1469787756005' // unix timestamp more than an hour in the past (access tokens expire after an hour)
});
plus.people.get({ userId: 'me', auth: oauth2Client }, function(err, response) {
  // handle err and response 
});

That worked for me!

分享给朋友:
您可能感兴趣的文章:
随机阅读: