3

我正在尝试使用 Amazon 设备地址 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 来部署技能并使用烧瓶请求进行开发。以下是代码:

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)

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

4

2 回答 2

0

您应该使用 get_api_access_token 函数来检查权限:https ://github.com/alexa/alexa-cookbook/blob/90de4b0ccbf508c0e6d1ad16535e179004db2e10/feature-demos/skill-demo-device-location/lambda/py/lambda_function.py #L56

于 2020-09-14T14:55:18.380 回答
0

在您的实施中

TOKEN =  context.System.user.permissions.consentToken

consentToken为空,因为您无权访问用户的地址。

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

于 2018-03-20T21:33:20.590 回答