0
使用Google的google-auth-library Node Module時,我的刷新令牌無法正確重播請求。google-auth-library:刷新令牌無法重播請求
我收到的ERR回調的消息是:
Error: Invalid Credentials
我已經看到了解決這一問題的其他問題,但在那裏提出的解決方案並沒有解決我的問題。
詳情:
在下面的代碼中,假定的說法googleAccessToken的樣子:
{
"access_token": "AN ACCESS TOKEN",
"refresh_token": "A REFRESH TOKEN"
}
我的客戶的最低版本:
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);
}
);
}
};
確實!這正是我處理它的原因:) –