如何通过API获取/修改谷歌日历设置中的「Country」字段内容
获取/修改谷歌日历「Country」字段的Google API方法
谷歌日历中「常规-语言和区域」板块的「Country」字段属于用户级全局日历设置,可通过Google Calendar API v3的Settings资源进行读写操作,具体方法如下:
获取Country字段内容
- 权限要求:需申请
https://www.googleapis.com/auth/calendar.settings.readonlyOAuth 2.0权限 - API调用方式:调用
Settings.get接口,指定settingId=country - Python示例代码:
from googleapiclient.discovery import build from google.oauth2.credentials import Credentials # 替换为已完成授权的Credentials对象 creds = Credentials.from_authorized_user_file('token.json', ['https://www.googleapis.com/auth/calendar.settings.readonly']) service = build('calendar', 'v3', credentials=creds) # 获取当前Country设置 country_setting = service.settings().get(settingId='country').execute() print(f"当前国家代码: {country_setting['value']}")
- 返回值说明:响应体的
value字段返回ISO 3166-1 alpha-2格式的国家代码(如US、JP)
修改Country字段内容
- 权限要求:需申请
https://www.googleapis.com/auth/calendar.settingsOAuth 2.0权限(权限级别高于只读) - API调用方式:调用
Settings.update接口,指定settingId=country并传入符合格式的新国家代码 - Python示例代码:
from googleapiclient.discovery import build from google.oauth2.credentials import Credentials # 替换为已完成授权的Credentials对象 creds = Credentials.from_authorized_user_file('token.json', ['https://www.googleapis.com/auth/calendar.settings']) service = build('calendar', 'v3', credentials=creds) # 更新国家代码为中国(CN) updated_setting = service.settings().update( settingId='country', body={'value': 'CN'} ).execute() print(f"更新后的国家代码: {updated_setting['value']}")
- 注意事项:传入的国家代码必须是有效的ISO 3166-1 alpha-2格式,否则API会返回
400 Bad Request错误
内容的提问来源于stack exchange,提问作者Konstantin
相关产品推荐
相关产品推荐

