问题描述
我是 Alexa 技能开发的新手,我正在尝试使用 Alexa 回复我的电子邮件的技能.
I'm new in Alexa Skill development and I'm trying to do a skill in which Alexa answers with my email.
我正在开发 Java 技能,我刚刚能够获取用户会话 ID:
I'm developing the skill in Java and I've just been able to take the user session id with:
getSession().getUser().getUserId()
获取 amzn1.ask.account.{id}
作为解决方案
问题是需要获取用户邮箱(例如:[email protected])
The problem is that a need to get the user email (example: [email protected])
有什么方法可以做到吗?
Is there any method to do it?
感谢您的帮助!
推荐答案
正如 Priyam Gupta 所说,这是用 api.amazon.com/user/profile?access_token= 和代码解决我以前解决的是:
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");
使用 JSON 方法:
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();
}
}
这篇关于如何在 Alexa Skill 中使用 Java 获取亚马逊用户电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!