首页 > 解决方案 > 在 Google Calendar API 的上下文中,404 是什么意思?

问题描述

我正在尝试使用Google Calendar APIlist以编程方式访问我拥有的日历:

r = requests.get(
        url="https://www.googleapis.com/calendar/v3/calendars/<the ID of my calendar which looks like mydomain_randomstuff@group.calendar.google.com>/events",
        params={
            'key': <the key from the API console>,
            'singleEvents': True,
            'orderBy': 'startTime'
        }

此调用失败并显示404

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "notFound",
    "message": "Not Found"
   }
  ],
  "code": 404,
  "message": "Not Found"
 }
}

在此 API 的上下文中,该错误实际上意味着什么?

笔记:

标签: google-apigoogle-calendar-api

解决方案


{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "notFound",
    "message": "Not Found"
   }
  ],
  "code": 404,
  "message": "Not Found"
 }
}

可以表示两件事之一。您输入的日历 ID 正确,或者您通过身份验证的用户无权访问该日历。确保您使用有权访问该日历的正确用户登录。或者,您可以执行calendar.list,它将返回用户当前有权访问的日历列表。这样您就不必担心可能会错过输入日历 ID。

授权

您使用events.list的方法需要授权(用户许可)才能访问他们的日历。可以在文档页面中看到

在此处输入图像描述

您需要使用 Oauth2 和上述范围之一来验证您的用户。然后,您将拥有一个可用于访问此日历的访问令牌。

apikey

API 密钥用于访问公共数据。除非您的日历设置为公开,否则您将无法使用它来查看活动。另请记住,api 密钥无权更新公共日历,您仍需要通过身份验证才能对其进行更改。

服务帐号

如果这是服务器到服务器应用程序,您应该使用服务帐户而不是 API 密钥。您需要做的就是将服务帐户添加为谷歌日历上的用户,就像您添加任何其他用户一样。然后它将可以访问您的日历。


推荐阅读