繁体   English   中英

获取并显示 Google Calender api (Python) 的事件 ID

[英]Get and display event id for Google Calender api (Python)

在将代码添加到我的工作项目之前,我正在使用基本的快速入门程序来测试代码,但我在如何检索事件 ID 并显示它们时遇到了问题。 一旦我管理了这一步,我将存储它们并使用 ID 删除事件。

这是我的错误:

event = service.events().get(calendarId='primary', eventId='eventId').execute() 文件“/Library/Python/2.7/site-packages/googleapiclient/_helpers.py”,第 130 行,在positional_wrapper 返回wrapped(*args, **kwargs) File "/Library/Python/2.7/site-packages/googleapiclient/http.py", line 856, in execute raise 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()

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

您正在使用 String 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是 Event 对象的一个​​属性。 您可以在此处查看其余属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM