简体   繁体   English

如何使用证书从颤振应用程序发送 AWS iot http 请求?

[英]How to send AWS iot http request from flutter app using certificates?

I am trying to publish a message to AWS IoT using flutter application.我正在尝试使用 Flutter 应用程序向 AWS IoT 发布消息。

Based on AWS's documentation I have tried to replicate根据我尝试复制的 AWS 文档

curl --tlsv1.2 \
    --cacert Amazon-root-CA-1.pem \
    --cert device.pem.crt \
    --key private.pem.key \
    --request POST \
    --data "{ \"message\": \"Hello, world\" }" \
    "https://IoT_data_endpoint:8443/topics/topic?qos=1"

Here is what I have tried这是我尝试过的

    final List<int> trustedCertificateBytes =
        (await rootBundle.load('assets/certificates/AmazonRootCA1.pem'))
            .buffer
            .asInt8List();
    final List<int> certificateChainBytes = (await rootBundle.load(
            'assets/certificates/device.pem.crt'))
        .buffer
        .asInt8List();
    final List<int> privateKeyBytes = (await rootBundle.load(
            'assets/certificates/private.pem.key'))
        .buffer
        .asInt8List();
    final data = jsonEncode(
      <String, dynamic>{"message": "hello world"},
    );


    final context = SecurityContext.defaultContext;
    context.setTrustedCertificatesBytes(trustedCertificateBytes);
    context.useCertificateChainBytes(certificateChainBytes);
    context.usePrivateKeyBytes(privateKeyBytes);
    final client = HttpClient(context: context);
    final request = await client.openUrl(
        'POST', Uri.parse("https://$myEndpoint/topics/some_topic?qos=1"));
    request.write(data);

    try {
      final response = await request.close();
      print("success");
      print(response);
      response.transform(utf8.decoder).listen((contents) {
        print(contents);
      });
    } catch (e) {
      print(e);
    }

On first api call, http call is successfull but the response is {"message":"Missing authentication","traceId":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx"}第一次调用 api 时,http 调用成功,但响应为{"message":"Missing authentication","traceId":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx"}

On every subsequent call flutter throws this error:在随后的每次调用中,flutter 都会抛出此错误:

 [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: TlsException: Failure trusting builtin roots (OS Error: CERT_ALREADY_IN_HASH_TABLE(x509_lu.c:356), errno = 0)

Any idea on how do I get it to work?关于如何让它工作的任何想法?

-- Edited section: The first call issue is solved, added context.setAlpnProtocols(["x-amzn-http-ca"], false); --编辑部分:解决了第一次调用的问题,添加了context.setAlpnProtocols(["x-amzn-http-ca"], false); worked like charm.像魅力一样工作。 The problem on subsequent call still exists.后续调用问题依然存在。

OP here.在这里。

Setting alpn protocol for the context solved the authentication token issue为上下文设置 alpn 协议解决了身份验证令牌问题

...
context.setAlpnProtocols(["x-amzn-http-ca"], false);
...

Then I set up a global flag on whether or not the certificates are already set.然后我设置了一个关于证书是否已经设置的全局标志。 If already set, no need to set again.如果已经设置,则无需再次设置。

Global scope全球范围

bool setCert = false;

Function scope功能范围

if (!setCert){
  // Set certificates & alpn protocol here
  setCert = true;
}

And it solved the second issue.它解决了第二个问题。

I think it's because you are adding and then re-adding the certs to SecurityContext.defaultContext on subsequent calls.我认为这是因为您正在添加证书,然后在后续调用中将证书重新添加到SecurityContext.defaultContext

You could try creating a new SecurityContext to do it each time.您可以尝试每次创建一个新的SecurityContext来执行此操作。

var sec = SecurityContext();
sec.set....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Dio 或 http 在 Flutter 中通过 GET 请求发送参数 - How to send parameters with GET request in Flutter using Dio or http 如何使用 flutter http 请求发送 CSRF 令牌 - how to send CSRF token using flutter http request Flutter - 如何在 Dart 中使用 HTTP 发送 POST 请求? - Flutter - How to send a POST request using HTTP in Dart? 如何将数据发送到互联网 HTTP 使用 flutter 发布请求(已修复) - how to send data to the internet HTTP Post request using flutter ( fixed ) 如何通过 Flutter 应用程序在 AWS 上自动创建独特的 IOT Thing? - How to create a unique IOT Thing on AWS automatically through a Flutter app? 如何在正文中发送嵌套的 Json 作为 Flutter 的 http 发布请求 - How to send nested Json in body as http post request from Flutter Flutter HTTP 请求发送使用 xml 主体 - Flutter HTTP request send using xml body 如何从flutter应用程序向本地主机发出http get请求? - How to make http get request from flutter app to the localhost? 如何从 Flutter 应用程序向 PHP 服务器发送 POST 请求 - How to send POST request from Flutter app to PHP server 如何使用 http POST 方法将当前用户(使用应用程序的人)Firebase Id 令牌从 Flutter 应用程序发送到 restapi? - How to send current user's(person using the app), Firebase Id token,from the Flutter app, to restapi, using http POST method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM