为了便于更新我们信息屏幕上的内容,我们正在考虑将内容放在Google云端硬盘上,然后允许通过基于NodeJS的应用程序同步内容。
目前我正在尝试使用自己帐户共享的文件夹来测试此方法。
到目前为止,基于https://github.com/google/google-api-nodejs-client/tree/master的文档:
var google = require('googleapis');
var OAuth2 = google.auth.OAuth2;
var readline = require('readline');
const CLIENT_ID = 'xxxxxxxx.apps.googleusercontent.com';
const CLIENT_SECRET = '7h3c13n7s3cr37';
const REDIRECT_URL = 'https://accounts.google.com/o/oauth2/auth';
var oauth2Client = new OAuth2(
CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);
var scopes = [
'https://www.googleapis.com/auth/drive.readonly',
'https://www.googleapis.com/auth/drive.metadata.readonly'
];
var url = oauth2Client.generateAuthUrl({
access_type: 'offline', // 'online' (default) or 'offline' (gets refresh_token)
scope: scopes // If you only need one scope you can pass it as string
});
console.log('past following URL into a web browser');
console.log(url);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Provide Key generated on web page ', (answer) => {
// store response key in file?
var drive = google.drive({ version: 'v2', auth: oauth2Client });
var folderId = 'mif01d3r';
drive.children.list({
auth: answer,
folderId: folderId,
}, function(error, response) {
if (error) {
console.log('err: ', error);
return;
}
console.log(response);
});
});
此处的当前问题是' REDIRECT_URL'似乎不合适。我应该把这个用于命令行应用程序?
答案 0 :(得分:2)
有一种更简单的方法。手动生成刷新令牌,然后从您的应用程序中引用(当然安全)。这样就不需要auth,没有重定向URL等等。
请参阅How do I authorise an app (web or installed) without user intervention? (canonical ?)
答案 1 :(得分:1)
https://developers.google.com/api-client-library/python/auth/installed-app#choosingredirecturi更适用于Python,但以下内容适用于所有"已安装的应用程序"
<强>瓮:IETF:WG:OAuth的:2.0:OOB 强>
此值向Google授权服务器发出信号,告知授权代码应在浏览器的标题栏中返回,页面文本提示用户复制代码并将其粘贴到应用程序中。当客户端(例如Windows应用程序)无法在没有重要客户端配置的情况下侦听HTTP端口时,这非常有用。
另见: https://developers.google.com/identity/protocols/OAuth2InstalledApp#choosingredirecturi
所以它应该是:
const REDIRECT_URL = 'urn:ietf:wg:oauth:2.0:oob';