【问题标题】:How to fix .get() error creating google calendar event with python?如何修复使用 python 创建谷歌日历事件的 .get() 错误?
【发布时间】:2019-10-20 05:24:45
【问题描述】:

我正在尝试使用 python 在谷歌日历中创建一个事件。错误在于使用 .get() 似乎存在问题。我对 google calendar api 不是很有经验,因为这是我的第一个程序,所以我无法再缩小范围,所以有人可以帮我解决这个问题吗?

我重新创建了视频中建议的代码 https://developers.google.com/calendar/create-events 我将 storage.json 更改为 credentials.json

触发错误的行是这个,

store = file.Storage('credentials.json')
creds = store.get()

该文件无法运行并吐出一堆错误。

这些是错误:

Traceback (most recent call last):
  File "goal_insert.py", line 9, in module>
    creds = store.get()
  File "/Users/name/Library/Python/2.7/lib/python/site-packages/oauth2client/client.py", line 407, in get
    return self.locked_get()
  File "/Users/name/Library/Python/2.7/lib/python/site-packages/oauth2client/file.py", line 54, in locked_get
    credentials = client.Credentials.new_from_json(content)
  File "/Users/name/Library/Python/2.7/lib/python/site-packages/oauth2client/client.py", line 302, in new_from_json
    module_name = data['_module']
KeyError: '_module'

【问题讨论】:

  • 你检查过这个相关的SO post吗?同时,您可以尝试将creds = store.get() 替换为creds = None 的解决方案。
  • @jess 我注释掉了该行和以下行,它似乎有效。但是,这会导致每次运行时都会更改凭据文件的问题。所以我最终读取了最初的 JSON 文件,在程序完成后,它会将该信息转储回 JSON 文件,使其处于与开始时相同的状态。但是,我不确定这是否会在我对此进行扩展时起作用,所以我想我可能需要修复它。我正在查看另一个线程 rn,看看我可以从中使用什么。

标签: python json google-calendar-api google-oauth google-apis-explorer


【解决方案1】:

我不知道为什么,但是注释掉了

creds = store.get() 

line 和之后的 line 让它工作,我不知道为什么说实话,但它修复了它。 这似乎改变了凭证文件,然后我可以取消注释这些行并且它再次工作。

【讨论】:

    【解决方案2】:

    我的建议是您应该从这里的快速入门开始:

    https://developers.google.com/calendar/quickstart/python

    还有关于如何创建事件的说明:

    https://developers.google.com/calendar/create-events

    您应该能够将这两个示例放在一起运行您的代码。下面是一个按照快速启动说明(安装 python、pip、google 库并将 credentials.json 文件与脚本放在同一目录中)并将创建事件功能添加到快速启动脚本的实现示例:

    from __future__ import print_function
    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 Admin SDK Directory API.
        Prints the emails and names of the first 10 users in the domain.
        """
        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)
    
        serviceCalendar = build('calendar', 'v3', credentials=creds)
    
        #Create event with invitees    
        event = {
            'summary': 'Liron clone wars training',
            'location': 'Barcelona city',
            'description': 'A chance to hear more about Google\'s developer products.',
            'start': {
                'dateTime': '2019-06-08T00:00:00',
                'timeZone': 'America/Los_Angeles',
            },
            'end': {
                'dateTime': '2019-06-08T08:00:00',
                'timeZone': 'America/Los_Angeles',
            },
            'attendees': [{"email": "random1@domain.eu"}, {"email": "random2@domain.eu"}]
        }
    
        event = serviceCalendar.events().insert(calendarId='primary', body=event).execute()
        print('Event created: %s' % (event.get('htmlLink')))
    
    if __name__ == '__main__':
        main()
    

    【讨论】:

      猜你喜欢
      • 2013-06-26
      • 2018-04-30
      • 2017-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多