Google-api-nodejs-client: .getToken يعيد valid_request

تم إنشاؤها على ٢٧ يوليو ٢٠١٤  ·  3تعليقات  ·  مصدر: googleapis/google-api-nodejs-client

تظهر لي رسالة خطأ في كل مرة أتصل فيها بـ getToken باستخدام رمز تصديق صالح. هنا سجل الخطأ المطبوع عبر الكود أدناه:

Error getting tokens:  invalid_request

هل هناك خطأ ما في طريقة استخدامي لواجهات برمجة التطبيقات؟ نقدر أي مؤشرات ...

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

ما ورد أعلاه يبدو جيدًا ولكن لماذا أحصل على رد اتصال مع تعيين خطأ على أمر غير صالح؟ ربما شيء خاطئ مع الناقل؟

ما هي محتويات googleClientInfo.web.redirect_uris ؟ هل هو خيط؟ إذا كانت مصفوفة ، فحاول تحديد القيمة الأولى في المصفوفة بدلاً من ذلك مثل googleClientInfo.web.redirect_uris[0] . إذا كنت تحصل على رمز مصادقة صالح ، فأنا لست متأكدًا مما إذا كان ذلك مهمًا.

ربما يمكنك محاولة الخداع باستخدام https://developers.google.com/oauthplayground/ ومقارنة طلباتك بالطلبات التي يرسلها الرمز الخاص بك.

نعم هذا يعمل! هل تعتقد أنه من الجيد إلقاء خطأ إذا كان URI عبارة عن مصفوفة؟ سيوفر وقت التصحيح لروح فقيرة مثلي :). شكرا جزيلا على مساعدتك. هتافات.

هل كانت هذه الصفحة مفيدة؟
0 / 5 - 0 التقييمات