【问题标题】:Google OAuth getting new access token with refresh tokenGoogle OAuth 使用刷新令牌获取新的访问令牌
【发布时间】:2015-11-26 07:50:33
【问题描述】:

我正在尝试让我的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)
        }   
    });
}

【问题讨论】:

    标签: node.js post oauth google-oauth gmail-api


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 根据OAuth 2.0 docs,正确的URL应该是https://www.googleapis.com/oauth2/v3/token
    • 他们可能都工作,你可以测试上面的网址。
    【解决方案2】:

    这行得通..

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

    【讨论】:

      猜你喜欢
      • 2014-07-15
      • 2017-06-09
      • 1970-01-01
      • 2019-06-29
      • 2013-09-05
      • 1970-01-01
      • 2013-11-05
      • 2015-07-19
      • 2019-03-16
      相关资源
      最近更新 更多