gpt4 book ai didi

python - Alexa 技能 : Get user location

转载 作者:行者123 更新时间:2023-11-28 19:03:52 24 4
gpt4 key购买 nike

我正在尝试使用亚马逊设备地址 API 获取设备的位置。我引用了这个代码的答案:Get location from Alexa Skills Kit (ASK)

但是,当我运行代码时,我得到一个 AttributeError: 'NoneType' object has no attribute 'consentToken'。以下是堆栈跟踪:

Traceback (most recent call last):
File "C:\Python36\lib\site-packages\flask\app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python36\lib\site-packages\flask\app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "C:\Python36\lib\site-packages\flask\app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python36\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Python36\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python36\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python36\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Python36\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Python36\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python36\lib\site-packages\flask\app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Python36\lib\site-packages\flask_ask\core.py", line 767, in _flask_view_func
result = self._map_intent_to_view_func(self.request.intent)()
File "C:\Python36\myfiles\redditreader.py", line 32, in share_headlines
location = get_alexa_location()
File "C:\Python36\myfiles\redditreader.py", line 14, in get_alexa_location
TOKEN = context.System.user.permissions.consentToken
AttributeError: 'NoneType' object has no attribute 'consentToken'

我正在使用 ngrok 部署技能和 flask-ask 进行开发。以下是代码:

from flask import Flask
from flask_ask import Ask, statement, question, session, context
import json
import requests
import time
import unidecode

app = Flask(__name__)
ask = Ask(app, "/reddit_reader")

def get_alexa_location():
URL = "https://api.amazonalexa.com/v1/devices/{}/settings" \
"/address".format(context.System.device.deviceId)
TOKEN = context.System.user.permissions.consentToken
HEADER = {'Accept': 'application/json',
'Authorization': 'Bearer {}'.format(TOKEN)}
r = requests.get(URL, headers=HEADER)
if r.status_code == 200:
return(r.json())

@app.route('/')
def homepage():
return "hi there, how ya doin?"

@ask.launch
def start_skill():
welcome_message = 'Hello there, would you like the news?'
return question(welcome_message)

@ask.intent("YesIntent")
def share_headlines():
location = get_alexa_location()
city = "Your City is {}! ".format(location["city"].encode("utf-8"))
address = "Your address is {}! ".format(location["addressLine1"].encode("utf-8"))
speech = city + address
return statement(speech)

@ask.intent("NoIntent")
def no_intent():
bye_text = 'I am not sure why you asked me to run then, but okay... bye'
return statement(bye_text)

if __name__ == '__main__':
app.run(debug=True)

我无法对上面链接的问题的答案发表评论,因为我还没有必要的声誉。感谢您的帮助!

最佳答案

在你的实现中

TOKEN =  context.System.user.permissions.consentToken

consentToken 为空,因为您没有访问用户地址的权限。

您需要先向亚马逊开发者门户网站 ( https://developer.amazon.com/edw/home.html#/skills ) 请求用户地址的许可。

关于python - Alexa 技能 : Get user location,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49394126/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com