Yogesh
Yogesh

Reputation: 424

Alexa flask-ask get the user personal information

I had setup Alexa skill to use Flask-Ask. Skill works fine as expected. Now I need to get the user personal information like name,phone,email,address.So I had used the below code

@ask.intent('AskPermission')
def get_permission():
    token=context.System.apiAccessToken
    api_end_point=context.System.apiEndpoint
    request_id=request.requestId
    headers = {"Content-Type": "application/json",
               # "X-Amzn-RequestId":request_id,
               "Authorization": "Bearer {}".format(token)}
    body = {
              "version": "1.0",
              "response": {
                "card": {
                  "type": "AskForPermissionsConsent",
                  "permissions": [
                    "alexa::profile:email:read",
                  ]
                }
              }
            }

    permission_result = requests.get('{api_end_point}/v2/accounts/~current/settings/Profile.email'.format(api_end_point=api_end_point), json=body, headers=headers)

I am getting 403 in response everytime. I am testing it on alexa simulator. I had enabled the permission inside the skills like below enter image description here

So am I doing anything wrong here

Upvotes: 1

Views: 793

Answers (3)

adam shamsudeen
adam shamsudeen

Reputation: 1872

You dont have to pass a body to the api:

@ask.intent('AskPermission')
def get_permission():
    """ Retreive User email from alexa user profile api """

    token = context.System.apiAccessToken
    api_end_point = context.System.apiEndpoint
    headers = {
        "Host": "api.amazonalexa.com",
        "Accept": "application/json",
        "Authorization": "Bearer {}".format(token)}

    resp = requests.get('{api_end_point}/v2/accounts/~current/settings/Profile.email'.format(api_end_point=api_end_point),headers=headers)
    if resp.status_code == 200:
        return resp.json()
    return resp

Upvotes: 0

Yogesh
Yogesh

Reputation: 424

Just for anyone,who has stuck on the same point. I was testing it from the alexa simulator. So to grant the permission you have to Go to https://alexa.amazon.in/spa/index.html#cards. This url is not mention in any documentation.

Select Skills (from left menu) => Your skills => All skills Grant the permission.

Below video has helped me to do this. https://www.youtube.com/watch?v=2Xfn5kNWbnU

Upvotes: 0

johndoe
johndoe

Reputation: 4387

Enabling permission in skill configuration means that the skill can ask for a consent from the user to access certain information. In your case, email Id. Once you configure that permission, the user has to grant access to that particular information. It's just like how you provide permission in Android apps. There are two ways in which a user can grant permission to your skill:

  1. Users will have to explicitly grant access to your skill under Settings menu of your Alexa skill.
  2. You can send a permission card to the user for consent.

Before you hit the apiEndpoint with the apiAccessToken you should have access to that information. Otherwise you would get 403. So whenever you get 403, send the user back a permission card. And once the user grants you permission, hit the apiEndpoint for the information you need.

Upvotes: 1

Related Questions