将calendarID从'primary'更改时出错?

问题描述 投票:1回答:1

我想在我的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()

如果name =='main':main()

enter image description here

enter image description here

python google-calendar-api
1个回答
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
  }
 ]
}

请使用idMyCal作为日历ID,然后重试。

References:

如果我误解了你的问题并且这不是你想要的结果,我道歉。

© www.soinside.com 2019 - 2024. All rights reserved.