要从Google Calendar API获取活动ID,您需要执行以下步骤:
Google Calendar API 是一个允许开发者访问和操作 Google 日历数据的 RESTful API。通过这个 API,您可以读取、创建、更新和删除日历事件。
以下是一个使用 Python 和 Google Calendar API 获取活动 ID 的示例:
import os.path
import pickle
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
# If modifying these SCOPES, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
def get_google_calendar_service():
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
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)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('calendar', 'v3', credentials=creds)
return service
def get_event_ids():
service = get_google_calendar_service()
events_result = service.events().list(calendarId='primary', maxResults=10).execute()
events = events_result.get('items', [])
event_ids = [event['id'] for event in events]
return event_ids
if __name__ == '__main__':
event_ids = get_event_ids()
print(f'Event IDs: {event_ids}')
credentials.json
文件正确无误。token.pickle
文件存在且有效。通过以上步骤和示例代码,您应该能够成功获取 Google Calendar API 中的活动 ID。
领取专属 10元无门槛券
手把手带您无忧上云