如何解决除了Google的代码段以外,还可以创建日历事件?
我试图通过Google API调用创建日历事件。根据{{3}},我必须使用:
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.readonly']
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'])
if __name__ == '__main__':
main()
但是,我正在构建一种Alexa技能,其中在调用handler_input.request_envelope.context.system.user.access_token
之后,alexa已经提供了access_token 。因此,我将代码调整为以下形式:
import os
import pickle
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'Timer_Hello_Layered/creds.json'
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
event = {
'summary': 'Test','location': 'At home','description': 'A chance to hear more about Google\'s developer products.','start': {
'dateTime': '2020-07-29T12:12:10','timeZone': 'America/Los_Angeles',},'end': {
'dateTime': '2020-07-29T12:32:47',}
}
#way of retrieving access token with alexa skill
creds = handler_input.request_envelope.context.system.user.access_token
#have to add /tmp/ when dealing with AWS lambda
SCreds = pickle.dump(creds,open("/tmp/save.pickle","wb"))
service = build('calendar',credentials= SCreds,cache_discovery=False)
event = service.events().insert(calendarId='primary',body=event).execute()
我意识到使用此代码,由于未在任何地方传递SCOPES或creds.json
文件,因此未创建事件(depsite出现否错误)。但是,与此同时,alexa不允许我运行creds = flow.run_local_server(port=0)
。
也有这个document,但我不知道它是否可以解决。即使是,也没有将creds.json传递给的参数。
有什么办法解决吗? 非常感谢您的帮助,因为我已经在此库存了2个多星期:((
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。