谷歌日历API - 如何在不必提示登录的情况下提出请求?



我有一个简单的问题:

我正在开发一个网站,需要完全授权才能向 Google 日历提出请求。我设法从Web服务器使用javascript完成我需要的所有请求,并且可以工作,但是我需要登录到我的Google帐户才能正常工作。这给使用我的网站的其他用户带来了问题,因为如果他们没有登录我的谷歌帐户,请求将不起作用。

我明白为什么它不起作用,我的问题是我怎样才能让我的网站获得使用谷歌日历的完全访问权限,而无需登录我的谷歌帐户,如果没有人必须登录谷歌帐户来执行任务,那就更好了?

您当前使用的登录形式称为 Oauth2。 它要求用户对访问权限进行身份验证。

您应该使用的是服务帐户。 服务帐户是预先授权的。 您需要与服务帐户共享您的个人日历,然后它才能访问它。

唯一的缺点是JavaScript不支持服务帐户身份验证,例如.js您需要切换到服务器端语言,例如node。

'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 中翻录的代码

相关内容

最新更新