我刚刚将我的第一个卑鄙的应用程序移到了AWS上,并考虑到一切进展顺利。但是我的问题是,现在我启动了它,并且运行我的Google Calendar API无法正常工作。我检查了日志,发现自从更改URL以来,该api要通过访问
重新验证现在,从日志中,我可以轻松地获取URL并进行验证,但是它提供了代码,并指示应用返回输入代码以进行验证。当它在AWS Elastic beantalk上运行时,该怎么办?
任何帮助将不胜感激。 谢谢
答案 0 :(得分:0)
如本文档所述:https://developers.google.com/calendar/quickstart/nodejs,您需要使用此新代码获取新令牌。
以下示例可能会为您提供帮助:
/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
* @param {google.auth.OAuth2} oAuth2Client The OAuth2 client to get token for.
* @param {getEventsCallback} callback The callback for the authorized client.
*/
function getAccessToken(oAuth2Client, callback) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
console.log('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter the code from that page here: ', (code) => {
rl.close();
oAuth2Client.getToken(code, (err, token) => {
if (err) return callback(err);
oAuth2Client.setCredentials(token);
// Store the token to disk for later program executions
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
if (err) console.error(err);
console.log('Token stored to', TOKEN_PATH);
});
callback(oAuth2Client);
});
});
}
答案 1 :(得分:0)
根据我的理解,您已经在日历API中注册了重定向URL,并且在登录页面后开始验证过程后,控制流便会使用所有凭据(令牌)重定向到该URL。
您可以在此处在节点应用程序中创建端点,例如/token
,并将其注册到日历API中。重定向发生后,您可以将数据保存在节点应用程序中的地图内,以后可用于所有其他日历API调用。
这是令牌端点的一个小例子:
var google = require("googleapis");
var OAuth2 = google.auth.OAuth2;
var sessMap = require('./sessionMap');
var oauth2Client = new OAuth2(<client_id>, <access_token>, <domain>+"/token");
var app = require('express')();
app.all('/token', function(req, res){
var code = req.query.code;
oauth2Client.getToken(code, function(err, tokens) {
if (err) {
res.send(err);
return;
}
oauth2Client.setCredentials(tokens);
//updated after sessionMap
sessMap.set('googleAuth', tokens);
res.send("This page will now redirect you");
});
});
要保存凭据,您可以创建如下所示的会话映射:
sessionMap.js
var hashmapSession = {};
exports.sess = {
set : function(key, value){
hashmapSession[key] = value;
},
get : function(key){
return hashmapSession[key];
},
all : function(){
return hashmapSession;
},
delete : function(key){
delete hashmapSession[key];
}
};
以上代码将必须合并到您的节点应用程序中。