-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AwsMobileClient is not initialising automatically in 2.7.1 #2400
Comments
@ZubairAkber AWSMobileClient and Amplify v2 are incompatible with each other. Amplify for Android v2 is considered an upgrade of Amplify for Android v1 and the AWS Android SDK. v2 migrates data from v1/sdk and they use a different local storage mechanism to securely store credentials. |
@tylerjroach i am using aws-android-sdk-iot for the MQTT broker and in case of amplify v2 it doesn't gets connected because it doesn't get the required information from AwsMobileClient object, what do you suggest me to do as i want to use v2 and iot from aws-android-sdk both together. Below is the code that i use to connect to MQTT
What additional steps do i take to make it work as it was working for v1 |
@ZubairAkber Looking at the iOT implementation, it just requires an AWSCredentialsProvider. Instead of using AWSMobileClient (mobile client dependency needs to be removed completely), you will want to create an implementation for the AWSCredentialsProvider interface. You can see examples of how to do this from a similar interface we have on Amplify v2 (https://github.com/aws-amplify/amplify-android/blob/d0dd5e6b5b8dd388a24850f016adc3ab73993075/aws-core/src/main/java/com/amplifyframework/auth/CognitoCredentialsProvider.kt). It doesn't translate directly the the AWSCredentialsProvider you will need to implement but should give a rough guide on how to do so. In summary, you should be able to use aws-android-sdk-iot with Amplify v2, as long as you remove the mobile client sdk. |
Thanks @tylerjroach, i will try and let you know. Can you please guide me a bit more, do i have to set it in the AWSIotMqttManager class using setCredentialsProvider method or there is any other way of doing this
This method accepts com.amazonaws.auth.AWSCredentialsProvider but the package you have mentioned give me com.amplifyframework.auth.CognitoCredentialsProvider, i am a bit confuse is there any other things as well that needs to be updated ? |
AWSCredentialsProvider is just an interface:
We can implement our own version of this interface by using Amplify v2 to fetch credentials.
Your connect call would then take note: I've updated the snippet in this comment to the corrected one suggested later in the thread to prevent confusion. |
Thanks @tylerjroach i will try and let you know |
@tylerjroach i have tried using the above solution but i am getting this error now when i try to connect AWSIotMqttManager W onFailure: connection failed. |
@ZubairAkber Please check that the time on your device is accurate. Please see this thread for more details: aws-amplify/aws-sdk-android#2674. |
@tylerjroach time is accurate as automatic date and time is enabled and it works fine with version 1 only version 2 is causing this issue |
I have observed the same. I'll investigate and provide a follow up. |
@ZubairAkber Please try the below credentials provider. I have verified this to work on an IoT project sample I have set up. IoT requires a
|
Hi @tylerjroach I've face the same issue. When I create a custom credential provider as you mention, some I would get a exception if I wait for a while and call Here is my code: class CognitoAWSCredentialsProvider() : AWSCredentialsProvider {
var profile = Profile()
override fun getCredentials(): AWSCredentials {
val latch = CountDownLatch(1)
var sdkCredentials: AWSCredentials? = null
try {
Amplify.Auth.fetchAuthSession(
{ authSession ->
sdkCredentials = ((authSession as AWSCognitoAuthSession).awsCredentialsResult.value as? AWSTemporaryCredentials)?.let {
Timber.i("fetchSession sdkCredentials: awsSessionToken: ${it.sessionToken}, awsAccessKeyId: ${it.accessKeyId}, awsSecretKey: ${it.secretAccessKey}")
BasicAWSCredentials(
it.accessKeyId, it.secretAccessKey
)
}
Timber.i("sdkCredentials: ${sdkCredentials?.awsAccessKeyId}, ${sdkCredentials?.awsSecretKey}")
latch.countDown()
},
{
// can better handle this exception and pass it down to the throwing call below
latch.countDown()
}
)
} catch (e: Exception) {
Timber.e(e)
val builder = AuthFetchSessionOptions.builder()
builder.forceRefresh(true)
Amplify.Auth.fetchAuthSession(
builder.build(),
{ authSession ->
sdkCredentials = ((authSession as AWSCognitoAuthSession).awsCredentialsResult.value as? AWSTemporaryCredentials)?.let {
Timber.i("fetchSession sdkCredentials: awsSessionToken: ${it.sessionToken}, awsAccessKeyId: ${it.accessKeyId}, awsSecretKey: ${it.secretAccessKey}")
BasicAWSCredentials(
it.accessKeyId, it.secretAccessKey
)
}
Timber.i("sdkCredentials: ${sdkCredentials?.awsAccessKeyId}, ${sdkCredentials?.awsSecretKey}")
latch.countDown()
},
{
// can better handle this exception and pass it down to the throwing call below
latch.countDown()
}
)
}
// wait for fetchAuthSession to return
latch.await()
// return captured credentials or throw
return sdkCredentials ?: throw IllegalStateException("Failed to get credentials")
}
override fun refresh() {
// refresh
}
}
fun initIot(accessKey: String, secretKey: String, sessionToken: String) {
credentialsProvider = CognitoAWSCredentialsProvider()
if (::mqttManager.isInitialized) return
Timber.i("[Remote] init Mqtt.")
mqttManager = AWSIotMqttManager(parameter.identityId, EndPoint)
}
suspend fun attachPolicy() {
val attachPolicyReq = AttachPolicyRequest()
val serviceType = ServiceType.getServiceType(currentServiceType)
attachPolicyReq.policyName = serviceType.policy
Timber.i("attachPolicy id: ${parameter.identityId}")
attachPolicyReq.target = parameter.identityId
Timber.i("credentialsProvider.credentials: ${credentialsProvider.credentials.awsAccessKeyId}, ${credentialsProvider.credentials.awsSecretKey}")
val iotAndroidClient = AWSIotClient(credentialsProvider.credentials)
iotAndroidClient.setRegion(Region.getRegion(IotRegion)) // name of your IoT Region such as "us-east-1"
iotAndroidClient.attachPolicy(attachPolicyReq)
} And the exception which crash at attachPolicy: Is that mean I didn't change the credentials inside the |
@tylerjroach it gets connected using the new implementation of MyCredentialsProvider but is it the final solution to that issue |
@ZubairAkber I believe this provides everything you need. If you experience any other issues, please let us know. I'll mark as closing soon to give some time. @ininmm I have replied in the new issue that you created and will follow up with any additional questions there. |
|
Before opening, please confirm:
Language and Async Model
Kotlin
Amplify Categories
Authentication, REST API, DataStore
Gradle script dependencies
Environment information
Please include any relevant guides or documentation you're referencing
No response
Describe the bug
I was using 1.30.0 version of amplify and every thing works fine, when i move to 2.7.1 and updated the code amplify works fine i just get the data and ready state is triggered but when it tries to connect to MQTT i got the following error
MQTT broker: a3jmi0po6ccx5n-ats.iot.ap-southeast-1.amazonaws.com:443
MQTT I Connection Lost com.amazonaws.AmazonClientException: Cognito Identity not configured
System.err W com.amazonaws.AmazonClientException: Cognito Identity not configured
System.err W at com.amazonaws.mobile.client.AWSMobileClient.getCredentials(AWSMobileClient.java:391)
System.err W at com.amazonaws.mobileconnectors.iot.AWSIotMqttManager$1.run(AWSIotMqttManager.java:993)
System.err W at java.lang.Thread.run(Thread.java:1012)
What i have observed is AwsMobileClient is not initialised at the start of app when i use 2.7.1 and in case of 1.30.0 it is initialised properly at the start with the logs that its initialise is called.
Reproduction steps (if applicable)
Simply update to 2.7.1 version of amplify from 1.30.0
Code Snippet
// Put your code below this line.
Log output
amplifyconfiguration.json
No response
GraphQL Schema
Additional information and screenshots
No response
The text was updated successfully, but these errors were encountered: