Python 谷歌的替代方案';创建日历事件的代码段?

Python 谷歌的替代方案';创建日历事件的代码段?,python,google-api,google-calendar-api,alexa,alexa-skills-kit,Python,Google Api,Google Calendar Api,Alexa,Alexa Skills Kit,我正试图通过谷歌的API调用创建日历事件。根据我必须使用的: 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 i

我正试图通过谷歌的API调用创建日历事件。根据我必须使用的:

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技能,在调用
处理程序input.request\u envelope.context.system.user.access\u token
后,Alexa已经提供了访问令牌。因此,我将代码调整为如下内容:

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',
    'timeZone': 'America/Los_Angeles',
}
}
#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', 'v3', credentials= SCreds, cache_discovery=False)    
event = service.events().insert(calendarId='primary', body=event).execute()
我意识到,通过这段代码,事件没有被创建(depsite gettingnoerror),因为我没有在任何地方传递作用域或
creds.json
文件。但是,同时,alexa不允许我运行
creds=flow.run\u local\u服务器(port=0)

也有这个,但我不知道这是否是解决办法。即使是,也没有creds.json要传递到的参数

这有什么办法吗?
我非常感谢您的帮助,因为我已经在这两个多星期了:((

我非常怀疑alexa能否创建Google访问令牌。请编辑您的代码,并包含用于创建其他访问令牌的代码。DaImTo alexa允许开发人员使用他们想要的任何工具。这只是API调用,代码甚至可以存在于不同的位置。下面是一个使用Google API创建Al的示例exa Skill:那么您正试图通过Alexa Skill在Google calendar中创建日历事件?如果您正在从帐户获取访问令牌,将您的技能链接到您的Google帐户,您需要在链接时传递范围,因为返回的Google令牌与您在身份验证时请求的范围绑定。您无法添加稍后进行作用域,而无需重新验证并从用户处获得新的授权。@AhDev感谢这篇有用的文章!三个小问题:1:我的Web授权URI*在“测试阶段”为“”好吧,还是我把它改成亚马逊域名?第二:第21行和第22行是验证accessToken和其他google API通用性的alexa代码(即:日历)?尽管谷歌代码片段以不同的方式进行。最后,你在哪里传递谷歌的作用域?我们是否应该将其传递到帐户链接选项卡?我们是否可以在作用域栏中传递url作为作用域,因为我认为我们只能使用单个词(即openid,)@让MyPeopleCode是的!这很有道理。我的问题仍然是:我可以将作用域的url粘贴到“帐户链接”选项卡中吗?因为我认为我只能传递单个单词(即电子邮件、个人资料、openid)?如果不能,还有什么选择?