Search code examples
javaalexa-skills-kit

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!


Solution

  • 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();
        }
     }