Google-api-nodejs-client: .getToken 返回 invalid_request

创建于 2014-07-27  ·  3评论  ·  资料来源: googleapis/google-api-nodejs-client

每次我使用有效的 authCode 调用 getToken 时都会收到错误消息。 这是通过以下代码打印的错误日志:

Error getting tokens:  invalid_request

我使用 API 的方式有问题吗? 感谢任何指针...

var express = require('express');
var router = express.Router();
var google = require('googleapis');
var urlshortener = google.urlshortener('v1');
var OAuth2 = google.auth.OAuth2;
var plus = google.plus('v1');
var googleClientInfo = { "web": {...} };
var oauth2 = new OAuth2(googleClientInfo.web.client_id, googleClientInfo.web.client_secret, googleClientInfo.web.redirect_uris);
var scopes = [  'https://www.googleapis.com/auth/plus.me',
  'https://www.googleapis.com/auth/calendar'
];
google.options({ auth: oauth2 }); // set auth as a global default
var authUrl = oauth2.generateAuthUrl( {
  access_type: 'offline',
  scope: scopes
});

/* GET authCode from Google Server */
router.get('/', function(req, res) {
  console.log('Redirecting to URL: ' + JSON.stringify(authUrl));
  res.redirect(authUrl);
});

/* '/successful' is the redirect_uri with authCode query */
router.get('/successful', function(req, res) {
  console.log('Got code: ', (req.query.code));
  oauth2.getToken(req.query.code, function(err, tokens) {
    if(err) {
        console.log("Error getting tokens: ", err);
        res.redirect('/');
    } else {
        console.log("Saving tokens: ", JSON.stringify(tokens));
        // Save tokens
        oauth2.setCredentials(tokens);
        plus.people.get({ userId: 'me', auth: oauth2 }, function(err, response) {
          if(err) {
              console.log("Error getting userId: ", err);
              res.redirect('/');      
          } else {
              res.send('Login Successful: ' + JSON.stringify(response));
              console.log('Login Successful: ' + JSON.stringify(response));
          }
        });
    }
  });
});

module.exports = router;
triage me

最有用的评论

是的,这行得通! 如果 URI 是一个数组,你认为抛出错误是个好主意吗? 将为像我这样的可怜人节省调试时间:)。 非常感谢您的帮助。 干杯。

所有3条评论

更新:我在 oauth2client.js 中添加了一些日志记录代码,如下所示:

/**
 * Gets the access token for given code.
 * <strong i="6">@param</strong> {string} code The authorization code.
 * <strong i="7">@param</strong> {function=} opt_callback Optional callback fn.
 */
OAuth2Client.prototype.getToken = function(code, opt_callback) {
  var uri = this.opts.tokenUrl || OAuth2Client.GOOGLE_OAUTH2_TOKEN_URL_;
  var values = {
    code: code,
    client_id: this.clientId_,
    client_secret: this.clientSecret_,
    redirect_uri: this.redirectUri_,
    grant_type: 'authorization_code'
  };

 console.log("posting to uri: ", uri + ", with values: " + JSON.stringify(values));

  this.transporter.request({
    method: 'POST',
    uri: uri,
    form: values,
    json: true
  }, opt_callback);
};

我得到以下日志(我添加了一些* * 来隐藏用户信息

posting to uri:  https://accounts.google.com/o/oauth2/token, with 
values: {"code":"4/U2PMY1Gu6ImEuax57j9an7UoBBFa.or_RbQzmcKYZBrG_bnfDxpJe-8vsjgI", 
"client_id":"**********62-srmgmr0i8ck7j4vvi0454ntcfkoufgu1.apps.googleusercontent.com", 
"client_secret":"CdhVi3Yk21g_uswaDZafTeNC", 
"redirect_uri":["https://*********.herokuapp.com/googleLogin/successful"], 
"grant_type":"authorization_code"}

Error getting tokens:  invalid_request

上面看起来不错,但是为什么我会收到一个将 err 设置为 invalid_request 的回调? 运输机可能有问题?

googleClientInfo.web.redirect_uris的内容是什么? 它是一个字符串吗? 如果它是一个数组,请尝试指定数组中的第一个值,而不是像googleClientInfo.web.redirect_uris[0] 。 如果您获得有效的 authCode,那么我不确定这是否重要。

也许您可以尝试使用https://developers.google.com/oauthplayground/进行欺骗,并将您的请求与您的代码发送的请求进行比较。

是的,这行得通! 如果 URI 是一个数组,你认为抛出错误是个好主意吗? 将为像我这样的可怜人节省调试时间:)。 非常感谢您的帮助。 干杯。

此页面是否有帮助?
0 / 5 - 0 等级