我尝试使用 v3 日历 api、api 密钥和浏览器获取(或邮递员)查询任何给定日历的日历事件列表。我创建了一个测试日历“测试日历 2”,将其公开,创建了一个事件,为其添加了颜色,并在 google 控制台上创建了一个 api 密钥。
使用此获取网址:
https://www.googleapis.com/calendar/v3/calendars/CALENDAR_ID/events?key=MY_API_KEY
,我得到如下事件列表:
{
"kind": "calendar#events",
"etag": "\"_\"",
"summary": "Test Calendar 2",
"description": "",
"updated": "2024-04-06T04:47:30.237Z",
"timeZone": "America/Los_Angeles",
"accessRole": "reader",
"defaultReminders": [],
"nextSyncToken": "_==",
"items": [
{
"kind": "calendar#event",
"etag": "\"_\"",
"id": "_",
"status": "confirmed",
"htmlLink": "https://www.google.com/calendar/event?eid=_",
"created": "2024-04-05T04:12:13.000Z",
"updated": "2024-04-06T03:34:03.072Z",
"summary": "Hello",
"description": "Description",
"creator": {
"email": "[email protected]"
},
"organizer": {
"email": "[email protected]",
"displayName": "Test Calendar 2",
"self": true
},
"start": {
"dateTime": "2024-04-01T19:00:00-07:00",
"timeZone": "America/Los_Angeles"
},
"end": {
"dateTime": "2024-04-01T20:00:00-07:00",
"timeZone": "America/Los_Angeles"
},
"visibility": "public",
"iCalUID": "[email protected]",
"sequence": 0,
"eventType": "default"
}
]
}
但是,事件对象中的一些属性丢失了(最重要的是
colorId
)。如此处所述:https://developers.google.com/calendar/api/v3/reference/events#resource
我错过了什么?这个领域是否受到某种限制或者我可以让它工作吗?
在线阅读大量有关 colorId 字段的混合信息。我读到,如果事件只有默认颜色,则
colorId
字段可能未定义。但我更新了很多次,似乎没有返回任何内容。
问题在于
colorId
现在是 Google 日历事件的私有属性。要访问此字段,您需要按照以下步骤操作。
.../auth/calendar.events
。pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
credentials.json
。main.py
文件
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import os
import json
# The required OAuth2 scopes for accessing Google Calendar
SCOPES = ['https://www.googleapis.com/auth/calendar']
def get_credentials():
creds = None
# The file where the access and refresh tokens are stored after the first run
token_file = 'token.json'
if os.path.exists(token_file):
creds = Credentials.from_authorized_user_file(token_file, SCOPES)
# If there are no valid credentials, initiate the OAuth flow
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES) # Use the credentials.json file that contains your client_id and secret
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open(token_file, 'w') as token:
token.write(creds.to_json())
return creds
if __name__ == '__main__':
get_credentials()
print("OAuth flow completed, token saved to 'token.json'")
python main.py
token.json
文件。key={api_key}
,而应使用 token
文件中的 token.json
。Authorization: Bearer {token}
要刷新令牌,请使用以下请求(数据应取自
token.json
):
POST https://oauth2.googleapis.com/token
Content-Type: application/x-www-form-urlencoded
client_id={your_client_id}&
client_secret={your_client_secret}&
refresh_token={your_refresh_token}&
grant_type=refresh_token