谷歌OAuth使用刷新令牌获取新的访问令牌

5

我正在尝试使用request模块获取我的refresh_token以生成新的access_token,但是它返回了一个错误,大致意思是“找不到页面”。

var request = require('request');
module.exports = function(callback){
    console.log('here');
    request('https://googleapis.com/oauth2/v3/token?client_id=NotID&client_secret=Not_Secret&refresh_token=NotRefresh&grant_type=refresh_token', function (error, response, body) {
        if (!error && response.statusCode == 200) {
            callback(response)
        }   
    });
}
2个回答

5

试试这个:

request.post('https://accounts.google.com/o/oauth2/token', {
  form: {
    grant_type:'refresh_token',
    refresh_token:'..',
    client_id:'..',
    client_secret:'..'
  }
}, function (err, res, body) {})

1
根据OAuth 2.0文档,正确的URL应该是https://www.googleapis.com/oauth2/v3/token - abraham
它们可能都有效,您可以测试上面的URL。 - simo
它在我这里可行,更新网址为 https://www.googleapis.com/oauth2/v4/token - Irwan

1

这个有效。

const axios = require('axios');
const querystring = require('querystring');
const keys = require('../config/keys');

const getAccessToken = async refreshToken => {
  try {
    const accessTokenObj = await axios.post(
      'https://www.googleapis.com/oauth2/v4/token',
      querystring.stringify({
        refresh_token: refreshToken,
        client_id: keys.googleClientID,
        client_secret: keys.googleClientSecret,
        grant_type: 'refresh_token'
      })
    );
    return accessTokenObj.data.access_token;
  } catch (err) {
    console.log(err);
  }
};

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接