aws-amplify / aws-sdk-android

AWS SDK for Android. For more information, see our web site:
https://docs.amplify.aws
Other
1.03k stars 550 forks source link

AWSBasicSessionCredentialsProvider (IOS) alternative in Android - Question #510

Closed praveshkhatana closed 6 years ago

praveshkhatana commented 6 years ago

No Credential Provider for BasicSession in Android.

I have user's session credentials and want to pass it to mqttManager but unable to find any suitable provider which support it. IOS version has "AWSBasicSessionCredentialsProvider" which solves the purpose.

STSSessionCredentialsProvider required long-lived credentials.

IOS working Code

credentialsProvider =  new AWSBasicSessionCredentialsProvider(this.credentials.accessKeyId,this.credentials.secretAccessKey,this.credentials.sessionToken);
let iotEndPoint = new AWSEndpoint(this.IOT_ENDPOINT);

        // // Configuration for AWSIoT control plane APIs
        let iotConfiguration = new AWSServiceConfiguration(this.AWSRegion, credentialsProvider);

        // Configuration for AWSIoT data plane APIs
        let iotDataConfiguration = new AWSServiceConfiguration(this.AWSRegion,
                                                           iotEndPoint,
                                                           credentialsProvider);

        AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = iotConfiguration;

        this.iotManager = AWSIoTManager.defaultIoTManager();
        this.iot = AWSIoT.defaultIoT();

        AWSIoTDataManager.registerIoTDataManagerWithConfigurationForKey(iotDataConfiguration, this.ASWIoTDataManager);

        this.iotDataManager = AWSIoTDataManager.IoTDataManagerForKey(this.ASWIoTDataManager);
 this.iotDataManager.connectUsingWebSocketWithClientIdCleanSessionStatusCallback( this.clientId, true, (status)=>{

                                this.callback &&
                                    this.callback.onMqttEventCallback &&
                                    this.callback.onMqttEventCallback(status);
                            });

Android NON WORKING CODE :(.


let cred = new BasicSessionCredentials(accessKeyId,secretAccessKey,sessionToken);
let credentialsProvider = new STSSessionCredentialsProvider(cred);
mqttManager = new AWSIotMqttManager(clientId, CUSTOMER_SPECIFIC_ENDPOINT);
    mqttManager.setKeepAlive(10);

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

sunchunqiang commented 6 years ago

Hello @praveshkhatana

In your code snippet, you are mixing kotlin and java code.

In an Android project, you can pass 'BasicSessionCredentials' into 'AWSStaticCredentialsProvider(credentials)', like code below.

BasicSessionCredentials sessionCredentials = new BasicSessionCredentials(accessKeyId,secretAccessKey,sessionToken);

mqttManager = new AWSIotMqttManager(clientId, CUSTOMER_SPECIFIC_ENDPOINT);
mqttManager.setKeepAlive(10);

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

You can implement the class StaticCredentialsProvider by yourself like the code in https://github.com/aws/aws-sdk-android/blob/master/aws-android-sdk-core/src/main/java/com/amazonaws/internal/StaticCredentialsProvider.java

praveshkhatana commented 6 years ago

@sunchunqiang Thanks for reply, Yes this is what i did and it's working fine now.

kiranalpha commented 5 years ago

@praveshkhatana and @sunchunqiang can you provide full code for Android AWS IOT Web socket, i was try your code but it show only Reconnecting message.