1

我一直在尝试所有方法来让Amplify/PubSub正常工作,但没有任何运气。似乎所有文档都已经过时了。

这是我到目前为止所做的。请注意,所有哈希都是由组成的 ;-)

  1. 创建了一个全新的 React Native 应用程序
  2. 安装的放大包
  3. 安装的 Amplify CLI
  4. 调用$ amplify configure
  5. 调用$ amplify init
  6. 调用$ amplify add auth
  7. Invoked $ amplify push,它创建了aws-exports.js对象
  8. 创建了一个超级简单的组件
    import React from 'react';
    import { View } from 'react-native';

    import { withAuthenticator } from 'aws-amplify-react-native';
    import Amplify, { Auth, PubSub } from 'aws-amplify';
    import { AWSIoTProvider } from '@aws-amplify/pubsub/lib/Providers';

    import awsmobile from './aws-exports';

    Amplify.configure({
      Auth: {
        mandatorySignIn: true,
        region: awsmobile.aws_cognito_region,
        userPoolId: awsmobile.aws_user_pools_id,
        identityPoolId: awsmobile.aws_cognito_identity_pool_id,
        userPoolWebClientId: awsmobile.aws_user_pools_web_client_id,
      },
      Analytics: {
        disabled: true,
      },
    });

    Amplify.addPluggable(
      new AWSIoTProvider({
        aws_pubsub_region: 'ap-southeast-2',
        aws_pubsub_endpoint:
      'wss://a123456789d-ats.iot.ap-southeast-2.amazonaws.com/mqtt',
      })
    );

    Amplify.Logger.LOG_LEVEL = 'DEBUG';

    class App extends PureComponent {
      componentDidMount() {
        if (this.props.authState === 'signedIn') {
          Auth.currentCredentials().then((creds) => {

            // get the principal that needs to be attached to policy
            console.log('principal to be attached', creds.identityId)

            PubSub.subscribe('topic_1').subscribe({
              next: (data) => console.log(JSON.stringify(data, null, 2)),
              error: (msg) => console.log('ERROR: ', msg.error),
              close: () => console.log('Done'),
            });
          });
        }
      }

      render() {
        return (
          <View/>
        );
      }
    }

    export default withAuthenticator(App);
  1. 我将 AWS 根证书附加到我的 iPhone(见下文)

10.为 AWS IoT 创建 IAM 策略

  • IoTAppPolicy
  • iot:*
  • arn:aws:iot:ap-southeast-2:1234567890:*

11.将我得到的委托人附加Auth.currentCredentials到保单上

aws iot attach-principal-policy --policy-name IoTAppPolicy --principal ap-southeast-2:db1234bc-5678-90123-4567-89ae0e123b4

12.将政策附加到Auth Role

  • AWSIoTDataAccess
  • AWSIoTConfigAccess

然而,当我运行应用程序时,我收到以下错误日志

[DEBUG] 51:42.745 SignIn - Sign In for test@test.com
[DEBUG] 51:44.616 AuthClass CognitoUserSession {idToken: CognitoIdToken, refreshToken: CognitoRefreshToken, accessToken: CognitoAccessToken, clockDrift: 0}
[DEBUG] 51:44.616 Credentials - set credentials from session
[DEBUG] 51:46.247 Credentials - Load credentials successfully 
[DEBUG] 51:46.248 AuthClass - succeed to get cognito credentials 
DEBUG] 51:47.150 Hub - Dispatching to auth with  {event: "signIn", data: CognitoUser, message: "A user 2d...de has been signed in"}
[DEBUG] 51:47.151 SignIn CognitoUser {username: "2d...de", pool: CognitoUserPool, Session: null, client: Client, signInUserSession: CognitoUserSession, …}
[DEBUG] 51:47.152 AuthClass - Getting the session from this user: CognitoUser {username: "2d...de", pool: CognitoUserPool, Session: null, client: Client, signInUserSession: CognitoUserSession, …}
[DEBUG] 51:47.152 AuthClass - Succeed to get the user session CognitoUserSession {idToken: CognitoIdToken, refreshToken: CognitoRefreshToken, accessToken: CognitoAccessToken, clockDrift: 1}
[DEBUG] 51:47.574 AuthPiece - verified user attributes {verified: {…}, unverified: {…}}
[INFO] 51:47.575 Authenticator - Inside handleStateChange method current authState: signIn
[DEBUG] 51:47.575 VerifyContact - no unverified contact
[INFO] 51:47.578 Authenticator - authState has been updated to signedIn
[DEBUG] 51:47.581 AuthClass - getting current credentials
[DEBUG] 51:47.582 Credentials - getting credentials
[DEBUG] 51:47.582 Credentials - picking up credentials
[DEBUG] 51:47.582 Credentials - getting new cred promise
[DEBUG] 51:47.582 Credentials - checking if credentials exists and not expired
[DEBUG] 51:47.583 Credentials - credentials not changed and not expired, directly return
[DEBUG] 51:47.583 AnalyticsClass - Analytics has been disabled
[DEBUG] 51:47.584 PubSub - subscribe options undefined
[DEBUG] 51:47.584 MqttOverWSProvider - Subscribing to topic(s) topic1
[DEBUG] 51:47.584 Credentials - getting credentials
[DEBUG] 51:47.584 Credentials - picking up credentials
[DEBUG] 51:47.584 Credentials - getting new cred promise
[DEBUG] 51:47.585 Credentials - checking if credentials exists and not expired
[DEBUG] 51:47.585 Credentials - are these credentials expired?
[DEBUG] 51:47.585 Credentials - credentials not changed and not expired, directly return
[DEBUG] 51:47.586 Signer {region: "ap-southeast-2", service: "iotdevicegateway"}
[DEBUG] 51:47.590 MqttOverWSProvider - Creating new MQTT client cca4e07f-a15a-46ce-904d-483a83162018
[WARN] 52:50.152 MqttOverWSProvider - cca4e07f-a15a-46ce-904d-483a83162018 {
  "errorCode": 7,
  "errorMessage": "AMQJS0007E Socket error:undefined.",
  "uri": "wss://a123456789d.iot.ap-southeast-2.amazonaws.com/mqtt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ASIAU7VGXF6UOWZIUFOM%2F20200502%2Fap-southeast-2%2Fiotdevicegateway%2Faws4_request&X-Amz-Date=20200502T055147Z&X-Amz-SignedHeaders=host&X-Amz-Signature=010d8..7dba&X-Amz-Security-Token=IQ..lk%3D"
}
ERROR: Disconnected, error code: 7
[DEBUG] 52:50.153 MqttOverWSProvider - Unsubscribing from topic(s) topic1

知道为什么我无法连接到该主题吗?

4

1 回答 1

1

谁会想到一个包裹不见了。

安装后

yarn add amazon-cognito-identity-js

在我的物理 iPhone 上一切正常。

我去了AWS IoT Core页面并从那里发布了一条测试消息,瞧,我得到了以下对象

{
  "message": "Hello from AWS IoT console"
}
于 2020-05-02T07:13:54.610 回答