当前位置: 首页 > 知识库问答 >
问题:

无法使用android客户端连接aws iot mqtt代理

陶原
2023-03-14

下面是我在代码中所做的:

if (clientKeyStore == null) {
            Log.i(LOG_TAG, "Cert/key was not found in keystore - creating new key and certificate.");

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // Create a new private key and certificate. This call
                    // creates both on the server and returns them to the
                    // device.
                    CreateKeysAndCertificateRequest createKeysAndCertificateRequest =
                            new CreateKeysAndCertificateRequest();
                    createKeysAndCertificateRequest.setSetAsActive(true);
                    final CreateKeysAndCertificateResult createKeysAndCertificateResult;
                    createKeysAndCertificateResult =
                            mIotAndroidClient.createKeysAndCertificate(createKeysAndCertificateRequest);
                    Log.i(LOG_TAG,
                            "Cert ID: " +
                                    createKeysAndCertificateResult.getCertificateId() +
                                    " created.");

                    // store in keystore for use in MQTT client
                    // saved as alias "default" so a new certificate isn't
                    // generated each run of this application
                    AWSIotKeystoreHelper.saveCertificateAndPrivateKey(certificateId,
                            createKeysAndCertificateResult.getCertificatePem(),
                            createKeysAndCertificateResult.getKeyPair().getPrivateKey(),
                            keystorePath, keystoreName, keystorePassword);

                    // load keystore from file into memory to pass on
                    // connection
                    clientKeyStore = AWSIotKeystoreHelper.getIotKeystore(certificateId,
                            keystorePath, keystoreName, keystorePassword);


                    // Attach a policy to the newly created certificate.
                    // This flow assumes the policy was already created in
                    // AWS IoT and we are now just attaching it to the
                    // certificate.
                    AttachPrincipalPolicyRequest policyAttachRequest =
                            new AttachPrincipalPolicyRequest();
                    policyAttachRequest.setPolicyName(AWS_IOT_POLICY_NAME);
                    policyAttachRequest.setPrincipal(createKeysAndCertificateResult
                            .getCertificateArn());
                    mIotAndroidClient.attachPrincipalPolicy(policyAttachRequest);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            btnConnect.setEnabled(true);
                        }
                    });
                } catch (Exception e) {
                    Log.e(LOG_TAG,
                            "Exception occurred when generating new private key and certificate.",
                            e);
                }
            }
        }).start();
    }
}

View.OnClickListener connectClick = new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        Log.d(LOG_TAG, "clientId = " + clientId);

        try {
            mqttManager.connect(clientKeyStore, new AWSIotMqttClientStatusCallback() {
                @Override
                public void onStatusChanged(final AWSIotMqttClientStatus status,
                        final Throwable throwable) {
                    Log.d(LOG_TAG, "Status = " + String.valueOf(status));

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (status == AWSIotMqttClientStatus.Connecting) {
                                tvStatus.setText("Connecting...");

                            } else if (status == AWSIotMqttClientStatus.Connected) {
                                tvStatus.setText("Connected");

                            } else if (status == AWSIotMqttClientStatus.Reconnecting) {
                                if (throwable != null) {
                                    Log.e(LOG_TAG, "Connection error.", throwable);
                                }
                                tvStatus.setText("Reconnecting");
                            } else if (status == AWSIotMqttClientStatus.ConnectionLost) {
                                if (throwable != null) {
                                    Log.e(LOG_TAG, "Connection error.", throwable);
                                }
                                tvStatus.setText("Disconnected");
                            } else {
                                tvStatus.setText("Disconnected");

                            }
                        }
                    });
                }
            });
        } catch (final Exception e) {
            Log.e(LOG_TAG, "Connection error.", e);
            tvStatus.setText("Error! " + e.getMessage());
        }
    }
};

keystore中的证书没有读取并给我IoException。

共有1个答案

郑功
2023-03-14

您得到的错误是:

com.amazonaws.AmazonServiceException: User: arn:aws:sts::964546574005:assumed-role/Cognito_GTekPool3Unauth_Role/CognitoIdentityCredentials is not authorized to perform:

指示缺少对指定角色的权限。您必须附加一个有效的策略,该策略允许您调用服务上的CreateKeysandCertificeAPI。

谢谢,罗翰

 类似资料:
  • 我正在尝试连接到我的AWS S3存储桶,以便根据这些链接的说明上传文件。 http://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpJava.html http://docs.aws.amazon.com/AWSSdkDocsJava/latest/DeveloperGuide/credentials.html#credenti

  • 我无法获取上一个已知位置。我已经在谷歌控制台中启用了地理编码API和谷歌地点API的Android。我在清单文件中添加了 API 密钥: 但我不断在控制台中收到一条消息:“无法连接到Google API客户端:连接结果{状态代码=API_UNAVAILABLE,分辨率=空}” 更新 我使用谷歌示例 onConnected和onConnectionFailed不调用。 而且我也使用Android反应

  • 我无法连接到我的节点。js服务器。当通过节点使用http web服务器在本地运行它时,它工作得很好,但是当连接到外部时,它会加载<code>socket.io。js文件很好,但当尝试使用套接字时,它会从URL中删除端口,无法连接。 而不是在网络请求中执行此操作: http://external-domain.com:3000/socket.io/?EIO=3 它是这样做的: http://exte

  • 我正在尝试连接到运行Windows server的AWS服务器上的Hansoft服务器。 我尝试打开所有入站流量进行测试,但没有成功。我可以ping服务器,所以它在那里。 汉软服务器使用默认端口 50256。 我还能尝试什么? 启动下面的向导 1 安全组设置。 入站安全规则: 所有流量、所有协议、所有端口范围、源 0.0.0.0/0 RDP,TCP 协议,端口范围 3389,源 0.0.0.0/0

  • 我使用这个(Android TCP连接增强的)教程来创建简单的JAVA TCPServer和Android TCPClient。它工作得很好,但使用这段代码我只能同时将一个设备连接到服务器。我必须更改什么才能连接多个设备?

  • 问题内容: 一切工作正常,但问题是当我在地图上定位时什么都没发生我注册了我的谷歌地图,我在这里做了所有说明 http://code.google.com/intl/pl/android/add-ons/google- apis/mapkey.html 我可以看到地图,可以放大和缩小,但是当我搜索位置时,会在logcat中找到它 这是我的Java代码 有什么帮助吗?提前致谢 问题答案: 如果您在AP