我正在尝试使用颤振应用程序向 AWS IoT 发布消息。
根据 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"
这是我尝试过的
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);
}
在第一次 api 调用时,http 调用成功但响应为{"message":"Missing authentication","traceId":"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx"}
在随后的每次调用中,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)
关于如何让它工作的任何想法?
--编辑部分:第一次通话问题已解决,添加了context.setAlpnProtocols(["x-amzn-http-ca"], false);
魅力。后续调用问题依然存在。