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 評価