【发布时间】:2017-12-01 23:51:20
【问题描述】:
我正在使用自己的 gmail 用户来阅读公共日历。让程序在本地运行,并使用 console.log 显示凭据/令牌(更改值以保护我的令牌):
Got Token
OAuth2Client {
transporter: DefaultTransporter {},
_certificateCache: null,
_certificateExpiry: null,
_clientId: 'xxxxxxxxxxxxxx',
_clientSecret: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
_redirectUri: 'urn:ietf:wg:oauth:2.0:oob',
_opts: {},
credentials:
{ access_token: 'xxxxxxx',
refresh_token: 'xxxxxxxxxxxxxxxxxx',
token_type: 'Bearer',
expiry_date: 1512151860704 } }
我也做了 StackOverflow 所说的:How to oAuth Google API from Lambda AWS?,它给了我与上面显示的相同的 access_token。
所以,如果我理解的话,我需要获取访问令牌并将其放入我的程序或文件中,但我不知道该怎么做。我的代码来自此处的 Google 示例:https://developers.google.com/google-apps/calendar/quickstart/nodejs
我是把这个令牌放在我的 client_secret.json 文件中的某个地方还是什么?我尝试直接将它作为 TOKEN 的值传递给 listEvents 方法,但得到“400 Bad Request”。
更新 1: 我尝试将文件存储到磁盘,然后按如下方式读取:
exports.getCalendarJSONEventsNew =
function getCalendarJSONEvents(callback) {
console.log("started getCalendarJSONEventsNew");
fs.readFile('OAuth2Client.json', 'utf8',
function processedFile(err, content) {
if (err) {
console.log('Error loading OAuth2Client.json: ' + err);
return;
}
console.log("content=");
console.log(content);
var tokenFromFile = JSON.parse(content);
listEvents(tokenFromFile, function(jsonResult) {
console.log("Json Callback Events=");
console.log(jsonResult);
callback(jsonResult);
});
});
}
错误:它似乎并不完全是 JSON,所以不是如何将其反序列化回对象:
OAuth2Client {
^
SyntaxError: Unexpected token O in JSON at position 0
更新2:然后我有另一个想法,我将以下内容保存为
credentials: {
access_token: 'xxxxx',
refresh_token: 'xxxxxx',
token_type: 'Bearer',
expiry_date: 1512151860704
}
作为 .credentials/calendar-nodejs-quickstart.json。
然后当我在服务器上运行时,我得到了这个响应:
Authorize this app by visiting this url: https://accounts.google.com/o/oauth2/auth?etc...
【问题讨论】:
标签: amazon-web-services lambda oauth google-calendar-api