【问题标题】:Google Calendar Events Watch error 401 Unauthorized with service accountGoogle Calendar Events Watch 错误 401 Unauthorized with service account
【发布时间】:2019-07-21 02:33:34
【问题描述】:

我正在尝试查看多个日历以获取任何更改的推送通知:

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

【问题讨论】:

    标签: javascript google-api google-calendar-api service-accounts


    【解决方案1】:

    为了使服务帐户能够正常工作,它必须经过预授权。获取服务帐户电子邮件地址,并与其他用户一样与服务帐户共享日历。

    这也应该让您可以访问日历的推送通知。

    如果这不起作用,请确保服务帐户是创建初始监视请求的帐户。

    适用范围广

    日历使您可以完全访问用户日历,没有理由要求只读,也只需使用您需要的日历

    https://www.googleapis.com/auth/calendar",
    "https://www.googleapis.com/auth/calendar.readonly"
    

    服务帐号

    我不知道服务帐户可以与 javascript 一起使用,你的工作真是太棒了。

    【讨论】:

    • 您是否尝试过选择日历本身?您可以使用该服务帐户和 javascript 做任何事情吗?我真的不认为服务帐户会以客户端语言工作。
    • @DalmTo 我可以很好地授权我的网络令牌,我可以尝试我的主日历,我应该使用什么凭据?我已经更新了我原来的问题
    猜你喜欢
    • 2018-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 2018-07-23
    • 1970-01-01
    相关资源
    最近更新 更多