Google-api-nodejs-client: .getToken mengembalikan invalid_request

Dibuat pada 27 Jul 2014  ·  3Komentar  ·  Sumber: googleapis/google-api-nodejs-client

Saya mendapatkan kesalahan setiap kali saya memanggil getToken dengan authCode yang valid. Berikut adalah log kesalahan yang dicetak melalui kode di bawah ini:

Error getting tokens:  invalid_request

Apakah ada yang salah dengan cara saya menggunakan API? Hargai setiap petunjuk...

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

Komentar yang paling membantu

Ya itu berhasil! Apakah menurut Anda itu ide yang baik untuk membuat kesalahan jika URI adalah array? Akan menghemat waktu debug untuk jiwa yang malang seperti saya :). Terima kasih banyak atas bantuan Anda. Bersulang.

Semua 3 komentar

Pembaruan: Saya menambahkan beberapa kode logging di oauth2client.js sebagai berikut:

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

Saya mendapatkan log berikut (saya telah menambahkan beberapa * * untuk menyembunyikan info pengguna

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

Di atas terlihat baik-baik saja, tetapi mengapa saya mendapatkan panggilan balik dengan err disetel ke invalid_request? Mungkin ada yang salah dengan transporter?

Apa isi googleClientInfo.web.redirect_uris ? Apakah itu sebuah string? Jika ini adalah Array, coba tentukan nilai pertama dalam array sebagai gantinya seperti googleClientInfo.web.redirect_uris[0] . Jika Anda mendapatkan authCode yang valid maka saya tidak yakin apakah itu penting.

Mungkin Anda bisa mencoba membodohi dengan https://developers.google.com/oauthplayground/ dan membandingkan permintaan Anda dengan permintaan apa yang dikirim kode Anda.

Ya itu berhasil! Apakah menurut Anda itu ide yang baik untuk membuat kesalahan jika URI adalah array? Akan menghemat waktu debug untuk jiwa yang malang seperti saya :). Terima kasih banyak atas bantuan Anda. Bersulang.

Apakah halaman ini membantu?
0 / 5 - 0 peringkat