[英]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.