首页 > 解决方案 > 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);

Skill 还具有提醒权限: 技能页面开启提醒

但是当执行该方法时,我得到以下响应:

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 表示令牌有效但没有适当的权限。

也许你们中的一些人有同样的问题,或者可以帮助我弄清楚如何解决我的问题?谢谢

标签: amazonalexa

解决方案


得到它的工作,正如在这个答案中指出的那样,授予技能权限是不够的,使用该技能的最终用户还必须授予权限。遇到未经授权的响应时,通过权限卡向用户请求权限。


推荐阅读