amazon-web-services - AWS IOT - 凭证的范围应限于正确的服务

标签 amazon-web-services aws-sdk iot aws-iot

我正在尝试访问简单的 AWS IOT REST 服务,但尚未成功。这就是我所做的。

  1. 我在 AWS 中创建了一个 iam 用户并下载了访问 key 和 secret key
  2. 使用该用户登录 AWS IOT 并创建一个“事物”
  3. 从事物的属性中,我找到了影子的 REST URL
  4. 使用 Postman 和新的“aws 签名”功能,并为其提供访问 key 、 key 、区域 (us-east-1) 和服务名称 (iot)
  5. 尝试“获取”端点,这就是我得到的 -

    { "message": "凭证的范围应限于正确的服务。", “traceId”:“be056198-d202-455f-ab85-805defd1260d” }

  6. 我认为 postman 有问题,因此我尝试使用 aws-sdk-sample 连接到 S3 的示例,并将其更改为连接到 IOT URL。 这是我的程序片段(Java)

    String awsAccessKey = "fasfasfasdfsdafs";
    String awsSecretKey = "asdfasdfasfasdfasdfasdf/asdfsdafsd/fsdafasdf";
    
    URL  endpointUrl = null;
    String regionName = "us-east-1";
    try {
        endpointUrl = new URL("https://dasfsdfasdf.iot.us-east-1.amazonaws.com/things/SOMETHING/shadow");
    }catch (Exception e){
        e.printStackTrace();
    }
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("x-amz-content-sha256", AWSSignerBase.EMPTY_BODY_SHA256);
    
    AWSSignerForAuthorizationHeader signer = new AWSSignerForAuthorizationHeader(
            endpointUrl, "GET", "iot", regionName);
    String authorization = signer.computeSignature(headers,
            null, // no query parameters
            AWSSignerBase.EMPTY_BODY_SHA256,
            awsAccessKey,
            awsSecretKey);
    
    // place the computed signature into a formatted 'Authorization' header
    // and call S3
    headers.put("Authorization", authorization);
    String response = HttpUtils.invokeHttpRequest(endpointUrl, "GET", headers, null);
    System.out.println("--------- Response content ---------");
    System.out.println(response);
    System.out.println("------------------------------------");
    

这给了我同样的错误 -

--------- Request headers ---------
x-amz-content-sha256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
Authorization: AWS4-HMAC-SHA256 Credential=fasfasfasdfsdafs/20160212/us-east-1/iot/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=3b2194051a8dde8fe617219c78c2a79b77ec92338028e9e917a74e8307f4e914
x-amz-date: 20160212T182525Z
Host: dasfsdfasdf.iot.us-east-1.amazonaws.com
--------- Response content ---------
{"message":"Credential should be scoped to correct service. ","traceId":"cd3e0d96-82fa-4da5-a4e1-b736af6c5e34"}
------------------------------------

有人可以告诉我我做错了什么吗? AWS 文档没有提供有关此错误的太多信息。请帮忙

最佳答案

使用 iotdata 签署您的请求,而不是 iot
示例:

AWSSignerForAuthorizationHeader signer = new AWSSignerForAuthorizationHeader(
    endpointUrl, "GET", "iotdata", regionName);

关于amazon-web-services - AWS IOT - 凭证的范围应限于正确的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35369822/

相关文章:

python - 如何为在AWS EC2上运行的单个脚本安装第3方Python库?

node.js - S3.putObject - 回调永远不会被调用

c# - 用 C# 编写的 Windows IoT 控制台应用程序?

javascript - Ditto 中没有可用的消息映射处理器

http - 在 AWS-IoT 中使用 http 列出事物 api?

json - Cloudformation 中的 Cloudwatch 仪表板错误

amazon-web-services - WSS websocket 的 ELB 监听器配置

amazon-web-services - AWS CDK - 角色和策略创建

javascript - 与基础版本 SES 相比,SESv2 的 aws-sdk 是否缺少某些功能?

javascript - 获取AWS失败