【问题标题】:Amazon Lex authorization with Flutter使用 Flutter 进行 Amazon Lex 授权
【发布时间】:2020-07-17 17:06:15
【问题描述】:

我想编写与 Amazon Lex REST API 一起使用的 Flutter 应用程序。Amazon 有 authenticating 的特定方法我使用 SigV4 包来制作所需的标头

  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-web-services flutter amazon-lex


    【解决方案1】:

    我换了包,用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',
    

    在你的标题中。

    【讨论】:

      【解决方案2】:

      我遇到了类似的问题。我在 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"}),
      );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-01-23
        • 1970-01-01
        • 1970-01-01
        • 2020-10-06
        • 2015-10-07
        • 2015-07-17
        • 2015-10-16
        相关资源
        最近更新 更多