how to use the refreshAccessToken method to generate a new accesstoken in google-api-nodejs-client

浪子不回头ぞ 提交于 2019-12-31 01:34:53

问题


I see that this pull request adds a method to refresh the access token using a saved refresh token. It is not clear to me how to use it. I have saved the tokens (including the refresh token) from the original getToken request and am now retrieving the token from the Database in a new session. How do I set the credentials on OAuth2Client so that I can call refreshAccessToken and get a new accesstoken?


回答1:


I had missed the paragraph on setting credentials on the github readme, so here is some sample code in case anybody else needs it.

var googleOauth2Client = new OAuth2Client(googleClientId,googleClientSecret, googleCallbackUrl);
googleOauth2Client.setCredentials({
  refresh_token: saved_refresh_token
});
googleOauth2Client.refreshAccessToken(function(err, tokens){
  response.send({
    access_token: tokens.access_token
  });
});



回答2:


Just a guess as I haven't used this library. But it looks to me like you simply call myOAuth2Client.refreshAccessToken(function(err, newCredentials){}), where you have already instantiated the OAuth2Client object with the old token. (dunno how you do that but it might be as simple as instantiating the object then myOauth2Client.credentials.refresh_token = 'foobar'.) And if there's no error, it'll modify the OAuth2Client.credentials object and additionally pass the credentials object to the callback.



来源:https://stackoverflow.com/questions/19947663/how-to-use-the-refreshaccesstoken-method-to-generate-a-new-accesstoken-in-google

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!