์ ํจํ 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;
์ ๋ฐ์ดํธ: ๋ค์๊ณผ ๊ฐ์ด 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๊ฐ ๋ฐฐ์ด์ธ ๊ฒฝ์ฐ ์ค๋ฅ๋ฅผ ๋ฐ์์ํค๋ ๊ฒ์ด ์ข์ ์๊ฐ์ด๋ผ๊ณ ์๊ฐํ์ญ๋๊น? ๋์ ๊ฐ์ ๊ฐ๋ํ ์ํผ์ ์ํด ๋๋ฒ๊ทธ ์๊ฐ์ ์ ์ฝํ ์ ์์ต๋๋ค. :). ๋์์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค. ๊ฑด๋ฐฐ.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์, ์๋ํฉ๋๋ค! URI๊ฐ ๋ฐฐ์ด์ธ ๊ฒฝ์ฐ ์ค๋ฅ๋ฅผ ๋ฐ์์ํค๋ ๊ฒ์ด ์ข์ ์๊ฐ์ด๋ผ๊ณ ์๊ฐํ์ญ๋๊น? ๋์ ๊ฐ์ ๊ฐ๋ํ ์ํผ์ ์ํด ๋๋ฒ๊ทธ ์๊ฐ์ ์ ์ฝํ ์ ์์ต๋๋ค. :). ๋์์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค. ๊ฑด๋ฐฐ.