【问题标题】:Get and display event id for Google Calender api (Python)获取并显示 Google Calendar api (Python) 的事件 ID
【发布时间】:2020-04-09 11:10:36
【问题描述】:

在将代码添加到我的工作项目之前,我正在使用基本的快速启动程序来测试代码,但我在如何检索事件 ID 和显示它们方面遇到了麻烦。完成这一步后,我将存储它们并使用 ID 删除事件。

这是我的错误:

event = service.events().get(calendarId='primary', eventId='eventId').execute() 文件“/Library/Python/2.7/site-packages/googleapiclient/_helpers.py”,第 130 行,位于 positional_wrapper 返回包装(*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】:

    您正在使用字符串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 对象的一个​​属性。你可以看到here的其余属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-08
      相关资源
      最近更新 更多