Flutter 的 Amazon Lex 授权

Amazon Lex authorization with Flutter

我想编写与 Amazon Lex REST 一起使用的 Flutter 应用程序 API.Amazon 有特定的方法让 authenticating I used SigV4 包成为必需的 headers

  Sigv4Client client = Sigv4Client(
  keyId: kAccessKey,
  accessKey: kSecretKey,
  region: "us-east-1",
  serviceName: "lex",
);

final request = client.request(
  "https://runtime.lex.us-east-1.amazonaws.com/bot/myBotName/alias/BETA/user/myUserId/text",
  method: 'POST',
  body: jsonEncode({'inputText': 'hi'}),
);

var response=post(request.url, headers: request.headers, body: request.body);
print(response.body);

但我收到了这条消息:

{"message":"The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details."}

我在邮递员上得到了具有相同数据的有效响应,邮递员中唯一的部分是不同的,这个包是“X-Amz-Content-Sha256”值,当然还有签名值(每次都会改变)。 "X-Amz-Content-Sha256"邮递员值:

beaead3198f7da1e70d03ab969765e0821b24fc913697e929e726aeaebf0eba3

"X-Amz-Content-Sha256" 我的代码值:

ee9ef87bd5a357cff93b1d83d1e8a1b47fb3fa2e94251711c6a30250119e6369

我尝试编写函数来计算验证字符串,但它对我来说太复杂了。

我更改了包并使用amazon-cognito-identity-dart-2

并像这样发送请求:

AwsSigV4Client client=AwsSigV4Client(
      kAccessKey,
      kSecretKey,
      'https://runtime.lex.us-east-1.amazonaws.com',
      region: 'us-east-1',
      serviceName: 'lex',
    );

final signedRequest = new SigV4Request(
      client,
      method: 'POST',
      path: '/bot/MyBotName/alias/BETA/user/MyUser/text',
      headers: new Map<String, String>.from({
        'Content-Type': 'application/json; charset=utf-8',
        'ACCEPT': 'application/json',
      }),
      body: new Map<String, dynamic>.from({"inputText": "hi"}),
    );

var response = await http.post(
      signedRequest.url,
      headers: signedRequest.headers,
      body: signedRequest.body,
    );

添加

非常重要
'Content-Type': 'application/json; charset=utf-8',

在你的headers.

我遇到了类似的问题。我在 AwsSigV4Client 中设置了 defaultContentType: 'application/json; charset=utf-8' 并从 SigV4Request 中删除了 headers

AwsSigV4Client client=AwsSigV4Client(
  accessKey,
  secretKey,
  'https://runtime.lex.eu-west-2.amazonaws.com',
  region: 'eu-west-2',
  serviceName: 'lex',
  defaultContentType: 'application/json; charset=utf-8',
);

final signedRequest = new SigV4Request(
  client,
  method: 'POST',
  path: '/bot/MyBotName/alias/BETA/user/MyUser/text',
  body: new Map<String, dynamic>.from({"inputText": "hi"}),
);