在订阅日历中更改事件颜色时禁止响应 403

403 forbidden response when changing color of event in subscribed calendar

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

以下代码允许我为另一个非同步日历(我自己创建的)更改单个事件的颜色,但对于同步日历我得到错误 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()

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

我想过将日历复制到另一个我有写入权限的日历中,然后更改新日历的颜色,但这似乎有点矫枉过正。有更好的主意吗?

答案:

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

更多信息:

与个人共享日历有四个选项,在日历中可以看到UI:

除非此人有 Make changes to eventsMake changes and manage sharing 访问日历的权限,否则将收到此 Forbidden 回复。

注:

根据 handling Calendar API errors 上的文档:

403: Forbidden for non-organizer

The event update request is attempting to set one of the shared event properties in a copy that isn't the organizer's. Shared properties (for example, guestsCanInviteOthers, guestsCanModify, or guestsCanSeeOtherGuests) can only be set by the organizer.

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

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

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

参考文献: