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 ๋“ฑ๊ธ‰