You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过API获取/修改谷歌日历设置中的「Country」字段内容

获取/修改谷歌日历「Country」字段的Google API方法

谷歌日历中「常规-语言和区域」板块的「Country」字段属于用户级全局日历设置,可通过Google Calendar API v3的Settings资源进行读写操作,具体方法如下:

获取Country字段内容

  • 权限要求:需申请https://www.googleapis.com/auth/calendar.settings.readonly OAuth 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.settings OAuth 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 16:50:11