1
votes

So, I am using Amazon Alexa Reminders API as shown here. Here is my method for sending requests to 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();
    }
}

And I execute it like this:

RemindersToolkit.sendReminder(session.getUser().getAccessToken(), "text", 1);

Skill also has permission for reminders: Reminders are turned on on the skill's page

But when the method is executed, I get the following response:

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}

According to Amazon on this page, 401 UNAUTHORIZED means Token is valid but does not have appropriate permissions.

Maybe some of you guys had the same problem or could help me figure out how to solve mine? Thanks

2
Shot in the dark, but on my google home, you can't set a reminder unless the device knows who your are. IE google has to recognize my voice and associate it with an account so that it can send alerts to my device for example. Perhaps something similar is going on? To sanity check, can you post to other endpoints successfully w/ the same token? That error is not very informative :) - Adam Hughes
@AdamHughes I haven't heard of any similar voice recognition in Alexa - dddkk

2 Answers

1
votes

Got it worked, as pointed out in this answer, granting permission in the skill is not enough, the end user using that skill also has to grant the permission. Ask the user for the permission through permission card when encountered with the unauthorized response.

0
votes

The issue was that I was using the old Alexa SDK. I had to download current version of the SDK and there is a different key (not accessToken I've used), which can be obtained directly from the Intent object and can then be used to send requests