如何修复使用 python 创建 google 日历事件的 .get() 错误?
How to fix .get() error creating google calendar event with python?
我正在尝试使用 python 在 google 日历中创建一个事件。错误在于使用 .get() 似乎存在问题。我对 google 日历 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'
我不知道为什么,但是注释掉
creds = store.get()
行和之后的行使它起作用,老实说,我不知道为什么,但修复了它。
这似乎更改了凭据文件,然后我可以取消注释这些行并且它再次起作用。
我的建议是您应该从此处的快速入门开始:
https://developers.google.com/calendar/quickstart/python
还有关于如何创建事件的说明:
https://developers.google.com/calendar/create-events
您应该能够 运行 您的代码将这 2 个示例放在一起。这是一个遵循快速入门说明的实施示例(安装 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()
我正在尝试使用 python 在 google 日历中创建一个事件。错误在于使用 .get() 似乎存在问题。我对 google 日历 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'
我不知道为什么,但是注释掉
creds = store.get()
行和之后的行使它起作用,老实说,我不知道为什么,但修复了它。 这似乎更改了凭据文件,然后我可以取消注释这些行并且它再次起作用。
我的建议是您应该从此处的快速入门开始:
https://developers.google.com/calendar/quickstart/python
还有关于如何创建事件的说明:
https://developers.google.com/calendar/create-events
您应该能够 运行 您的代码将这 2 个示例放在一起。这是一个遵循快速入门说明的实施示例(安装 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()