AWS IOT - 凭证应限定于正确的服务。

22

我正在尝试访问一个简单的AWS IOT REST服务,但我还没有成功。以下是我所做的:

  1. 在我的AWS中创建了一个IAM用户,并下载了访问密钥和秘密密钥。
  2. 使用该用户登录AWS IOT并创建了一个"thing"。
  3. 从thing的属性中找到了阴影的REST URL。
  4. 使用带有新"aws signature"功能的Postman,并提供访问密钥、秘密密钥、区域(us-east-1)和服务名称(iot)。
  5. 试图"GET"该端点,但结果是:

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

  6. 我认为Postman可能有问题,因此尝试使用连接到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("------------------------------------");
    

这给了我同样的错误 -

--------- 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文档上关于这个错误的信息很少。请帮帮我。


你解决了这个问题吗?我也遇到了同样的问题。 - Vinicius Zilli Pavei
同样的问题在这里。 - MeV
4个回答

21

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

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

4
在使用Postman尝试做这件事时遇到了困难。关于“iotdata”的这一小部分在API文档中找不到 - 谢谢。 - kert
1
AWS的文档是我用过的最糟糕的。在我最终找到解决问题的小细节之前,我花了3天时间并进行了大量的试错。 - Mathyou

3
在第四步中,不需要填写任何“服务名称”。Postman将默认使用“execute-api”作为值。
希望这可以解决你的问题!

0

这基本上是由于服务名称未正确给出,您可以使用服务名称 =“iotdata”而不是iot。

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


-3

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


1
那真的没有回答被问到的问题。 - alanwill

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接