amazon - Alexa 提醒 API 401 响应
问题描述
因此,我正在使用 Amazon Alexa Reminders API,如此处所示。这是我向 API 发送请求的方法:
public static void sendReminder(String accessToken, String reminderText, long offsetInSec) {
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost("https://api.amazonalexa.com/v1/alerts/reminders");
post.addHeader("Authorization", "Bearer " + accessToken);
post.addHeader("Content-Type", "application/json");
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'");
df.setTimeZone(tz);
String nowAsISO = df.format(new Date());
String jsonContent = "{ \"requestTime\" : \"" + nowAsISO + "\", \"trigger\": { \"type\" : \"SCHEDULED_RELATIVE\", \"offsetInSeconds\" : \"" + offsetInSec + "\" }, \"alertInfo\": { \"spokenInfo\": { \"content\": [{ \"locale\": \"en-US\", \"text\": \"" + reminderText + "\" }] } }, \"pushNotification\" : { \"status\" : \"ENABLED\" } }";
HttpEntity entity = null;
try {
byte[] bytes = jsonContent.getBytes("UTF-8");
entity = new ByteArrayEntity(bytes);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
post.setEntity(entity);
try {
CloseableHttpResponse response = client.execute(post);
System.out.println(response);
} catch (IOException e) {
e.printStackTrace();
}
}
我像这样执行它:
RemindersToolkit.sendReminder(session.getUser().getAccessToken(), "text", 1);
但是当执行该方法时,我得到以下响应:
HttpResponseProxy{HTTP/1.1 401 Unauthorized [Content-Type: application/json, Connection: keep-alive, Server: Server, Date: Tue, 22 Jan 2019 00:21:21 GMT, Vary: Accept-Encoding,User-Agent, x-amz-rid: 8YMCM10GKVGTT71JQH3N, X-Cache: Error from cloudfront, Via: 1.1 05a90e634e0872685ad69ee9a4e0eba5.cloudfront.net (CloudFront), X-Amz-Cf-Id: J5CtMnkUTv1hd6p-7-tob7mCb-4DM7y_LxhEiMLt5x3qEqmzhwbx_Q==] org.apache.http.client.entity.DecompressingEntity@6df97b55}
根据 Amazon on this page, 401 UNAUTHORIZED 表示令牌有效但没有适当的权限。
也许你们中的一些人有同样的问题,或者可以帮助我弄清楚如何解决我的问题?谢谢
解决方案
得到它的工作,正如在这个答案中指出的那样,授予技能权限是不够的,使用该技能的最终用户还必须授予权限。遇到未经授权的响应时,通过权限卡向用户请求权限。
推荐阅读
- google-analytics - 我可以在自定义报告中显示与事件相关的综合浏览量吗?
- javascript - 正则表达式 - 输出字符无效
- amp-html - 是否可以自定义 AMP Stories 的分享按钮位置?
- javascript - 在另一个函数中使用异步等待中的返回值
- php - 替换扩展实体类型的“label.form.empty_value”
- java - 从短/字节缩小隐式转换
- excel - 如何根据与另一列的完全匹配对列进行排序
- flask - 带有 websockets 和 flask-socketio 的 Python 到 Python WebSockets
- java - 如果条件可以为真或假,为什么有必要
- jquery-selectors - Cheerio:选择多个输入的值