我想将事件添加到我的 Google 日历帐户上的特定日历中。如果我将 calendarid 从“primary”更改为远离“primary”,则会收到错误。
我想专门添加的日历称为“MyCal” 我尝试用“MyCal”替换“主要”。我还尝试公开我的日历并从共享链接复制 ID,但我仍然收到“404...未找到”错误。我正在使用示例代码。如果将 calendarid 设置为“primary”,则它可以工作。我添加事件的行位于最底部。我还附上了错误。谢谢!
from __future__ import print_function
import datetime
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/calendar']
def main():
"""Shows basic usage of the Google Calendar API.
Prints the start and name of the next 10 events on the user's calendar.
"""
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()
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('calendar', 'v3', credentials=creds)
# Call the Calendar API
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
print('Getting the upcoming 10 events')
events_result = service.events().list(calendarId='primary', timeMin=now,
maxResults=10, singleEvents=True,
orderBy='startTime').execute()
events = events_result.get('items', [])
if not events:
print('No upcoming events found.')
for event in events:
start = event['start'].get('dateTime', event['start'].get('date'))
print(start, event['summary'])
event = {
'summary': 'Google I/O 2015',
'location': '800 Howard St., San Francisco, CA 94103',
'description': 'A chance to hear more about Google\'s developer products.',
'start': {
'dateTime': '2019-05-28T09:00:00-07:00',
'timeZone': 'America/Los_Angeles',
},
'end': {
'dateTime': '2019-05-28T17:00:00-07:00',
'timeZone': 'America/Los_Angeles',
},
'reminders': {
'useDefault': False,
'overrides': [
{'method': 'email', 'minutes': 24 * 60},
{'method': 'popup', 'minutes': 10},
],
},
}
event = service.events().insert(calendarId='MyCal', body=event).execute()
如果名称 == '主要': main()
最佳答案
我认为您正在尝试使用summary
作为日历ID。虽然在 primary
的情况下,primary
可以用作日历 ID,因为 id
和 summary
是相同的,MyCal
的日历 ID 与 summary
不同。这样就出现了这样的错误。
所以请使用日历 ID。例如,作为多种方法之一,您可以在 Try this API 处检索日历 ID。 。当您点击“EXECUTE”按钮时,您可以看到以下结果。
{
"items": [
{
"id": "### calendar ID ###",
"summary": "MyCal"
},
{
"id": "### email ###",
"summary": "### email ###",
"primary": true
}
]
}
请使用 MyCal
的 id
作为日历 ID,然后重试。
引用文献:
如果我误解了您的问题并且这不是您想要的结果,我深表歉意。
关于python - 将我的 calendarID 更改为 'primary' 时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54719359/