首页 > 解决方案 > 更改订阅日历中事件颜色时的 403 禁止响应

问题描述

我正在尝试根据其位置更改 Google 日历事件的颜色。我要更改的事件是带有 webcal 链接的同步日历的一部分。我无法单独更改每个事件的颜色,但我可以设置日历的颜色。

以下代码允许我更改另一个非同步日历(我自己创建)的单个事件的颜色,但对于同步日历我得到错误403: ForbiddenHttpError: <HttpError 403 when requesting https://www.googleapis.com/calendar/v3/calendars/CALENDAR_ID/events/EVENT_ID?alt=json returned "Forbidden">

from apiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow

scopes = ['https://www.googleapis.com/auth/calendar']
flow = InstalledAppFlow.from_client_secrets_file("client_secret.json", scopes=scopes)
credentials = flow.run_console()

service = build("calendar", "v3", credentials=credentials)


calId = "my_calendar_id"
calendar = service.events().list(calendarId=calId).execute()


keyword = "my_location_keyword"
for event in calendar['items']: 
# I navigate through each event of the specified calendar
    if 'location' in event and event['location'].find(keyword): 
    # if the location exists and contains the keyword, then i change the color
        service.events().patch(calendarId=calId, eventId=event['id'], body={"colorId":10}).execute()

有没有办法在不触发禁止错误的情况下更改颜色?在我看来,颜色不应该受到授权的保护......

我想过将日历复制到另一个我可以写访问的日历中,并更改新日历的颜色,但这似乎有点矫枉过正。有更好的主意吗?

标签: pythongoogle-calendar-api

解决方案


回答:

Code: 403 Message: Forbidden错误表明尝试编辑日历事件的帐户没有对日历的编辑权限。您必须在日历共享设置中更改此设置。

更多信息:

与个人共享日历有四个选项,如日历 UI 中所示:

在此处输入图像描述

除非个人拥有Make changes to eventsMake changes and manage sharing访问日历,否则Forbidden将收到此响应。

笔记:

根据处理日历 API 错误的文档:

403:非组织者禁止

事件更新请求正在尝试在不属于组织者的副本中设置共享事件属性之一。共享属性(例如 、guestsCanInviteOthersguestsCanModifyguestsCanSeeOtherGuests只能由组织者设置。

虽然此错误的详细信息似乎与403: Forbidden您描述的更简单的错误不同,但这可能是由于 API 更改所致。如果您尝试更改此处指定的属性之一(guestsCanModify例如 ),而没有上述日历共享设置,则会返回相同的、更简单的 403 错误:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "forbidden",
    "message": "Forbidden"
   }
  ],
  "code": 403,
  "message": "Forbidden"
 }
}

因此,建议您检查日历的共享设置,因为colorId如果没有正确设置,非组织者的用户将无法编辑。

参考:


推荐阅读