从“ primary”更改日历ID时出错?

时间:2019-02-16 02:38:53

标签: python google-calendar-api

我想将事件添加到我的Google日历帐户中的特定日历。如果我将Calendarid更改为“ primary”,那么我会收到错误消息。

我要特别添加的日历称为“ MyCal” 我尝试用“ MyCal”替换“ primary”。我也尝试过公开日历并从共享链接中复制ID,但是仍然出现“ 404 ... Not Found”错误。我正在使用示例代码。如果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()

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

我认为您正在尝试使用summary作为日历ID。尽管在primary的情况下,primary可以用作日历ID,因为idsummary相同,但是MyCal的日历ID不同来自summary。这样,就会发生这种错误。

因此,请使用日历ID。例如,作为几种方法之一,您可以在Try this API处检索日历ID。单击“执行”按钮时,可以看到以下结果。

{
 "items": [
  {
   "id": "### calendar ID ###",
   "summary": "MyCal"
  },
  {
   "id": "### email ###",
   "summary": "### email ###",
   "primary": true
  }
 ]
}

请使用id中的MyCal作为日历ID,然后重试。

参考文献:

如果我误解了您的问题,而这不是您想要的结果,我深表歉意。