3
votes

I'm trying to get the device's location using the Amazon device address API. I referred to this answer for the code: Get location from Alexa Skills Kit (ASK)

However, I get a AttributeError: 'NoneType' object has no attribute 'consentToken' when I run the code. Following is the stacktrace:

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'

I am using ngrok to deploy the skill and flask-ask for development. Following is the code:

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)

I am unable to comment on the answer to the question linked above because I don't have the requisite reputation yet. Thank you for your help!

2
Did you resolve this issue? I've got my code set up to dump the entire event object coming from Alexa into a database for review. There you can manually see if the consent token is being sent.ChootsMagoots

2 Answers

0
votes

In you implementation

TOKEN =  context.System.user.permissions.consentToken

consentToken is null because you don't have permission to access address of the user.

You need to request permission from amazon developer portal (https://developer.amazon.com/edw/home.html#/skills) for user's address first.