获取和显示Google Calender API(Python)的事件ID

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

我正在使用基本的快速入门程序来测试代码,然后再将其添加到我的工作项目中,并且在获取事件ID和显示事件ID时遇到了麻烦。管理完此步骤后,我将存储它们并使用ID删除事件。

这是我的错误:

event = service.events()。get(calendarId ='primary',eventId ='eventId')。execute()在positional_wrapper中的第130行中的文件“ /Library/Python/2.7/site-packages/googleapiclient/_helpers.py”返回包装(* args,** kwargs)执行中的文件“ /Library/Python/2.7/site-packages/googleapiclient/http.py”,行856引发HttpError(resp,content,uri = self.uri)googleapiclient.errors.HttpError:https://www.googleapis.com/calendar/v3/calendars/primary/events/eventId?alt=json返回了“未找到”>

生成它的代码是:

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(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)

    # 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 = service.events().get(calendarId='primary', eventId='eventId').execute()

    print(event['summary'])

    #service.events().delete(calendarId='primary', eventId='4qvgpuca08lp3rki5vnuo7qp7r').execute()
if __name__ == '__main__':
    main()

我还很新,所以我相信这是一个愚蠢的错误。我尝试将eventID包装在service.events()。list部分中,但没有成功。

python google-calendar-api
1个回答
0
投票

您正在使用字符串eventId,您应该在其中具有实际的事件ID。因此,您可以在此处更改代码:

    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 = service.events().get(calendarId='primary', eventId='eventId').execute()

    print(event['summary'])

    #service.events().delete(calendarId='primary', eventId='4qvgpuca08lp3rki5vnuo7qp7r').execute()
if __name__ == '__main__':
    main()

为此:

    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'])
        service.events().delete(calendarId='primary', eventId=event['id']).execute()

if __name__ == '__main__':
    main()

这样,您已经在列出事件后删除了事件。注意eventId=event['id']的语法。 id是事件对象的属性。您可以看到其余的属性here

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