如何在 Alexa Skill 中使用 Java 获取亚马逊用户电子邮件

How to get amazon user email with Java in Alexa Skill

我是 Alexa Skill 开发的新手,我正在尝试使用 Alexa 回复我的电子邮件的技能。

我正在开发 Java 的技能,我刚刚能够通过以下方式获取用户会话 ID:

getSession().getUser().getUserId()

得到amzn1.ask.account.{id}作为解决方案

问题是需要获取用户邮箱(例如:username@gmail.com)

有什么方法可以做到吗?

感谢帮助!

As Priyam Gupta said, this is solved with api.amazon.com/user/profile?access_token= 而我用来解决它的代码是:

    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");

使用 JSON 方法:

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();
    }
 }
  1. 转到developer.amazon您已设置 alexa 技能的帐户,在选项卡 "Build" 下,您将找到左侧菜单 "Permissions" 并打开 "Customer Email Address" 权限。
  2. 转到https://alexa.amazon.com/spa/index.html#cards -> 技能 -> 你的技能 -> select 你的技能 -> 设置 -> 管理权限 -> 打开你在第 1 点打开的所需权限并保存
  3. 在意图处理程序中的 java 代码中 @Override public Optional<Response> handle(HandlerInput input) { UpsServiceClient upsServiceClient = input.getServiceClientFactory().getUpsService().getProfileEmail(); }

如果您需要详细说明,只需按照以下简单步骤操作即可:

  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. 对亚马逊个人资料进行 API 调用 API