java - 亚马逊波利 API

标签 java amazon-web-services okhttp amazon-polly

我正在尝试使用 Amazon Polly REST API。
有人可以帮我做这个吗?
我正在使用 Java 和 OkHttp3 并试过这个:

    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();

并收到“403 forbidden”的回答。然后我尝试在线执行此 POST 请求 on this service并收到了这个: enter image description here 我究竟做错了什么?我应该修理什么?谢谢!

亚马逊文档:link
请不要建议我使用 SDK!


我读了this , this , this , thisthis , 还是什么都不懂。

  1. 我知道我的 secretKey 是 secret 的,我需要使用加密。
  2. 我需要像描述的那样做请求here ,但我不明白如何提出自己的要求...... 我的参数: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, 正文request={...}, accessKey=..., secretKey=... ..... 以及如何使用 OkHttp 发出此请求?请帮忙!

最佳答案

addHeader("Authorization", "AWS <accessKey>:<secretKey>") 从未在 AWS 上完成。您的 secret key 是 secret

请求通过签名进行身份验证,使用一系列基于您的 key 的 HMAC-SHA 迭代。

有充分的理由不喜欢使用 SDK,但您需要阅读并理解生成签名的文档。

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

从上面的链接开始详细解释了签名过程。

所有地区的所有服务都支持 Signature Version 4。

关于java - 亚马逊波利 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44770155/

相关文章:

java - Golang - gomobile 基本脚本编译错误

java - 如果 try block 突然完成怎么办?

java - 如何找到对应于给定 LocalDateTime 和 ZoneId 的所有 Instants?

nginx - 在一个 aws ec2 实例上托管两个网站

java - 有没有办法隐藏应用程序网络调用以防止网络拦截

java - LinkedBlockingQueue 和 ConcurrentLinkedQueue 有什么区别?

batch-file - 在批处理脚本中使用 AWS Elastic Beanstalk 自定义变量

amazon-web-services - 配置文件中的无效配置:/home/hadoop/hive.config“

android - 如何在 OkHTTP3 中一个接一个地发送请求?

android - OkHttp 忽略日志记录拦截器