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

Google OAuth getting new access token with refresh token

我正在尝试让我的 refresh_token 生成一个新的 access_token。我正在使用请求模块发出请求,但它返回了一个错误,说明了 "Could not find page".

的内容
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)
        }   
    });
}

试试这个:

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

这有效..

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);
  }
};