python - 更改订阅日历中事件颜色时的 403 禁止响应
问题描述
我正在尝试根据其位置更改 Google 日历事件的颜色。我要更改的事件是带有 webcal 链接的同步日历的一部分。我无法单独更改每个事件的颜色,但我可以设置日历的颜色。
以下代码允许我更改另一个非同步日历(我自己创建)的单个事件的颜色,但对于同步日历我得到错误403: Forbidden
。
HttpError: <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 events
或Make changes and manage sharing
访问日历,否则Forbidden
将收到此响应。
笔记:
根据处理日历 API 错误的文档:
403:非组织者禁止
事件更新请求正在尝试在不属于组织者的副本中设置共享事件属性之一。共享属性(例如 、
guestsCanInviteOthers
或guestsCanModify
)guestsCanSeeOtherGuests
只能由组织者设置。
虽然此错误的详细信息似乎与403: Forbidden
您描述的更简单的错误不同,但这可能是由于 API 更改所致。如果您尝试更改此处指定的属性之一(guestsCanModify
例如 ),而没有上述日历共享设置,则会返回相同的、更简单的 403 错误:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "forbidden",
"message": "Forbidden"
}
],
"code": 403,
"message": "Forbidden"
}
}
因此,建议您检查日历的共享设置,因为colorId
如果没有正确设置,非组织者的用户将无法编辑。
参考:
推荐阅读
- swiftui - 文本视图中的 SwiftUI 默认格式化程序?
- here-api - Here Maps for Javascript:禁用 WHEEL_ZOOM 不会使页面可滚动
- oracle - 如何在 select 语句(Oracle 数据库)的案例中运行 for 循环
- django - 为什么我不能使用 Django Signals 重新加载 HTML 页面
- r - R中的Chloropeth地图无法解决,我认为问题在于合并数据
- mysql - 我的 PHP 文件不会将 HTML 数据插入到 MySql 数据库表中
- javascript - 在同一页面中提交 PHP 表单,包括侧导航菜单
- sql - 如何在 SQL Server 中创建为每个唯一组重置的移动平均值?
- github - Github Action:将长命令拆分为多行
- android - Flutter应用开发中的依赖问题