Alexa Skill:获取用户位置

时间:2018-03-20 21:07:50

标签: python alexa alexa-skills-kit flask-ask

我尝试使用亚马逊设备地址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)

我无法评论上述问题的答案,因为我还没有必要的声誉。谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

在您的实施中

TOKEN =  context.System.user.permissions.consentToken

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

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

答案 1 :(得分:0)