我正在尝试查看多个日历以获取有关任何更改的推送通知:
https://developers.google.com/calendar/v3/reference/events/watch
我不断收到错误消息:
请求失败,状态码为401
我正在使用一个服务帐户,该帐户已获得每个日历的访问权限。
我的配置是:
{
"type": "service_account",
"project_id": "eventapi-219011",
"private_key_id": "5ac83bf728fc9f3f635cec8096170573620dd388",
"private_key": "xxx",
"client_email": "googlecalendarservice@eventapi-219011.iam.gserviceaccount.com",
"client_id": "111757078202416067272",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/googlecalendarservice%40eventapi-219011.iam.gserviceaccount.com"
}
我已经验证了域。这是我的代码,关于如何获得访问权限的任何想法?
let { google } = require("googleapis");
let privatekey = require("../config.json");
let axios = require("axios");
let ids = [];
let promises = [];
async function main() {
const client = new google.auth.JWT(
privatekey.client_email,
null,
privatekey.private_key,
[
"https://www.googleapis.com/auth/calendar",
"https://www.googleapis.com/auth/calendar.readonly"
]
);
const token = await client.authorize();
let config = {
headers: {
"Content-Type": "application/json;charset=UTF-8",
Authorization: token.token_type + " " + token.access_token
},
type: "web_hook",
address: "https:/rguc-calendars.firebaseapp.com/notifications"
};
axios
.post(
"https://www.googleapis.com/calendar/v3/calendars/rguc.co.uk_5hbugn8f7a4j3g7p6im1r1s1co@group.calendar.google.com/events/watch",
config
)
.then(function(response) {
// console.log("latest" + response);
})
.catch(function(error) {
console.log(error);
})
}
main();
我可以使用以下方法获得日历列表:
const res = await client.request('https://www.googleapis.com/calendar/v3/users/me/calendarList');
但不是watch
,这是我的凭据,我应该使用哪个凭据?
API key 1
Oct 10, 2018
AIzaSyBkAEZGoQwB0f-Kmq4mCCzm93422up8oQw
OAuth 2.0 client IDs
Web client 1
793554220141-91kh36o41rq2b337d1fcl50lvbqesl36.apps.googleusercontent.com
Service account keys
5ac83bf728fc9f3f635cec8096170573620dd388 GoogleCalendarService
答案 0 :(得分:0)
要使服务帐户正常运行,必须预先授权。像获取其他任何用户一样,获取服务帐户的电子邮件地址并与服务帐户共享日历。
这还应该使您能够访问日历的推送通知。
如果这不起作用,请确保该服务帐户是创建初始监视请求的帐户。
在许多范围内
日历使您可以填充用户日历,没有理由只要求使用只读日历
https://www.googleapis.com/auth/calendar",
"https://www.googleapis.com/auth/calendar.readonly"
服务帐户
我不知道服务帐户是否可以使用javascript来工作,真棒。