JBarrioGarcía
JBarrioGarcía

Reputation: 184

How to get amazon user email with Java in Alexa Skill

I'm new in Alexa Skill development and I'm trying to do a skill in which Alexa answers with my email.

I'm developing the skill in Java and I've just been able to take the user session id with:

getSession().getUser().getUserId()

Getting amzn1.ask.account.{id} as solution

The problem is that a need to get the user email (example: [email protected])

Is there any method to do it?

Thanks for help!

Upvotes: 0

Views: 2054

Answers (3)

Andrey Esaulov
Andrey Esaulov

Reputation: 1

If you need detailed instructions, just follow these simple steps:

  1. Set up Security Profile for Login with Amazon
  2. Enable Account Linking in Amazon Developer Console
  3. Add Redirect URLs to Security Profile
  4. Make an API call to Amazon Profile API

Upvotes: -2

Rizwan Zia
Rizwan Zia

Reputation: 31

  1. Go to developer.amazon account where you have setup your alexa skill, under the tab "Build" you will find the left menu "Permissions" and switch on "Customer Email Address" permission.
  2. Go to https://alexa.amazon.com/spa/index.html#cards -> Skills -> Your Skills -> select your skill -> Settings -> Manage Permissions -> Switch on required permission you have switched on in point 1 and save
  3. in the java code inside your intent handler @Override public Optional<Response> handle(HandlerInput input) { UpsServiceClient upsServiceClient = input.getServiceClientFactory().getUpsService().getProfileEmail(); }

Upvotes: 1

JBarrioGarc&#237;a
JBarrioGarc&#237;a

Reputation: 184

As Priyam Gupta said, this is solved with api.amazon.com/user/profile?access_token= And the code I used to solve it is:

    String accessToken = requestEnvelope.getSession().getUser().getAccessToken();
    String url = "https://api.amazon.com/user/profile?access_token=" + accessToken;
    JSONObject json = readJsonFromUrl(url);
    String email = json.getString("email");
    String name = json.getString("name");

With JSON methods:

private static String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;
    while ((cp = rd.read()) != -1) {
      sb.append((char) cp);
    }
    return sb.toString();
  }

public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
    InputStream is = new URL(url).openStream();
    try {
      BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
      String jsonText = readAll(rd);
      JSONObject json = new JSONObject(jsonText);
      return json;
    } finally {
      is.close();
    }
 }

Upvotes: 1

Related Questions