我有一个简单的问题:
我正在开发一个需要完全授权才能向Google日历发出请求的网站。我设法通过网络服务器上的javascript做我需要的所有请求,但它确实有效,但我需要登录我的谷歌帐户才能使用。这对使用我网站的其他用户造成了问题,因为如果他们未登录 MY Google帐户,则该请求无效。
我理解为什么它不起作用,我的问题是我怎么能为我的网站获得授予使用谷歌日历的完全访问权限而无需登录我的谷歌帐户,更好的是如果没有人必须登录到谷歌帐户执行任务??
答案 0 :(得分:0)
您当前使用的登录形式称为Oauth2。它要求用户验证访问权限。
您应该使用的是服务帐户。服务帐户已获得预授权。您需要与服务帐户共享您的个人日历,然后才能访问它。
唯一的缺点是JavaScript不支持服务帐户身份验证,您需要切换到服务器端语言,例如node.js。
'use strict';
const {google} = require('googleapis');
const path = require('path');
/**
* The JWT authorization is ideal for performing server-to-server
* communication without asking for user consent.
*
* Suggested reading for Admin SDK users using service accounts:
* https://developers.google.com/admin-sdk/directory/v1/guides/delegation
*
* See the defaultauth.js sample for an alternate way of fetching compute credentials.
*/
async function runSample () {
// Create a new JWT client using the key file downloaded from the Google Developer Console
const client = await google.auth.getClient({
keyFile: path.join(__dirname, 'jwt.keys.json'),
scopes: 'https://www.googleapis.com/auth/drive.readonly'
});
// Obtain a new drive client, making sure you pass along the auth client
const drive = google.drive({
version: 'v2',
auth: client
});
// Make an authorized request to list Drive files.
const res = await drive.files.list();
console.log(res.data);
return res.data;
}
if (module === require.main) {
runSample().catch(console.error);
}
// Exports for unit testing purposes
module.exports = { runSample };
代码从smaples jwt
中删除