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

我试图让我的refresh_token生成一个新的access_token 。 我正在使用请求模块发出请求,但是它返回一个错误,提示“无法find页面”。

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

Solutions Collecting From Web of "Google OAuth使用刷新令牌获取新的访问令牌"

尝试这个:

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