Peter Samokhin
Peter Samokhin

Reputation: 874

Amazon Polly API

I am trying to use Amazon Polly REST API.
Can someone, please, help me to do this?
I am using Java and OkHttp3 and tried this:

    String url = "https://polly.us-west-2.amazonaws.com/v1/speech";
    String postBody = "{\"OutputFormat\":\"mp3\",\"Text\":\"Some text to listen\",\"TextType\":\"text\",\"VoiceId\":\"Joanna\"}";

    MediaType mediaType = MediaType.parse("application/json; charset=utf-8");

    OkHttpClient client = new OkHttpClient.Builder()
            .connectTimeout(30, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .build();



    Request request = new Request.Builder()
            .url(url)
            .addHeader("Authorization", "AWS <accessKey>:<secretKey>")
            .post(RequestBody.create(mediaType, postBody))
            .build();

    Response response = client.newCall(request).execute();

And received "403 forbidden" answer. Then I tried to do this POST request online on this service and received this: enter image description here What am I doing wrong? What should I fix? Thanks!

Amazon docs: link
And please, dont suggest me to use SDK !


I read this, this, this, this and this, and still understand nothing.

  1. I understand that my secretKey is secret and that I need to use encryption.
  2. I need to do request like as describes here, but I cant understand how to make my own request... My parameters: method=POST, host=polly.us-west-2.amazonaws.com, endpoint=https://polly.us-west-2.amazonaws.com/v1/speech, region=us-west-2, content-type=application/json, body of post request={...}, accessKey=..., secretKey=... ..... And how to make this request using OkHttp? Please, help!

Upvotes: 6

Views: 2316

Answers (1)

Michael - sqlbot
Michael - sqlbot

Reputation: 179462

addHeader("Authorization", "AWS <accessKey>:<secretKey>") is never done on AWS. Your secret key is secret.

Requests are authenticated by signing them, using a series of HMAC-SHA iterations based on your secret key.

There are valid reasons to prefer not to use the SDKs, but you'll need to read and understand the documentation for generating signatures.

When you use the AWS Command Line Interface (AWS CLI) or one of the AWS SDKs to make requests to AWS, these tools automatically sign the requests for you with the access key that you specify when you configure the tools. When you use these tools, you don't need to learn how to sign requests yourself. However, when you manually create HTTP requests to AWS, you must sign the requests yourself.

http://docs.aws.amazon.com/general/latest/gr/signature-version-4.html

The signing process is explained in detail, beginning with the link, above.

Signature Version 4 is supported by all services in all regions.

Upvotes: 2

Related Questions