【发布时间】:2017-09-11 20:49:54
【问题描述】:
我正在按照此示例访问 Google Sheets API:
https://developers.google.com/sheets/api/quickstart/nodejs
示例代码中包含以下获取新 oauth 令牌的方法。
function getNewToken(oauth2Client, callback) {
var authUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
});
console.log('Authorize this app by visiting this url: ', authUrl);
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter the code from that page here: ', function(code) {
rl.close();
oauth2Client.getToken(code, function(err, token) {
if (err) {
console.log('Error while trying to retrieve access token', err);
return;
}
oauth2Client.credentials = token;
storeToken(token);
callback(oauth2Client);
});
});
}
这在我的本地机器上运行良好(根据终端提示手动访问页面,并在命令行中输入代码)。但这似乎不切实际,并且不适用于 Heroku。有什么办法可以自动化吗?也许通过在 nodeJS 应用程序中获取 URL(和令牌)并以某种方式存储它?
提前致谢。
【问题讨论】:
-
我遇到了同样的问题。我们如何在没有服务器 CLI 访问的情况下输入
'Enter the code from that page here:'? -
@noogui 我一直在玩这个,但是关于如何在 oAuth2 请求中使用从服务帐户获得的令牌的文档非常少。有什么指点吗?
标签: node.js heroku google-api google-oauth google-docs-api