AWS IOT - Credential的范围应该是正确的服务

时间:2016-02-12 18:35:44

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

我正在尝试访问简单的AWS IOT REST服务,但我还没有成功完成。这就是我做的。

  1. 我在我的aws中创建了一个iam用户并下载了访问密钥和密钥
  2. 与该用户登录AWS IOT并创建了一个""
  3. 从the thing的属性中我找到了影子的REST URL
  4. 邮递员使用新的" aws签名"功能,并提供访问密钥,密钥,区域(us-east-1)和服务名称(iot)
  5. 试图" GET"端点,这就是我得到的 -

    { "message": "Credential should be scoped to correct service. ", "traceId": "be056198-d202-455f-ab85-805defd1260d" }

  6. 我认为邮递员有问题所以我尝试使用连接到S3的aws-sdk-sample示例并将其更改为连接到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("------------------------------------");
    
  7. 这给了我同样的错误 -

    --------- 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文档没有关于此错误的大量信息。请帮忙

4 个答案:

答案 0 :(得分:11)

如果iotdata,请使用iot与您的请求签名 例如:

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

答案 1 :(得分:0)

它基本上由于服务名称没有正确给出你可以使用服务Name ='iotdata'而不是iot。

如果您使用密钥管理,则服务名称将为kms。 对于EC2服务名称将是ec2等。

答案 2 :(得分:0)

在您的第 4 步中,不要为 Service Name 填写任何内容。 Postman 将默认使用 execute-api 的值。

希望这有效!

答案 3 :(得分:-1)

请改用AWS IoT SDK for Node.js。从here下载IoT控制台生成的私钥和客户端证书以及CA根证书。从示例目录中的脚本开始。