我正在尝试使用python在Google日历中创建事件。错误在于使用.get()似乎存在部分问题。我对Google日历api的使用经验不是很丰富,因为这是我的第一个程序,所以我无法再缩小范围了,所以有人可以帮助我吗?
我重新创建了视频中建议的代码,网址为 https://developers.google.com/calendar/create-events 然后我将storage.json更改为凭据.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'
答案 0 :(得分:0)
我不知道为什么,但是将
注释掉creds = store.get()
line和使其生效的行之后,我不知道为什么要诚实,但这已经解决了。 似乎更改了凭证文件,然后我可以取消注释这些行,然后它又可以工作了。
答案 1 :(得分:0)
我的建议是,您应该从此处的快速入门开始:
https://developers.google.com/calendar/quickstart/python
也有关于如何创建事件的说明:
https://developers.google.com/calendar/create-events
您应该能够将这两个示例放在一起运行代码。这是一个按照快速入门说明进行安装的实现示例(安装python,pip,google库,并将凭据.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()