aws-amplify / aws-sdk-android

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

Create Thing: Unauthenticated access is not supported for this identity pool. #3613

Closed mbahmani90 closed 3 months ago

mbahmani90 commented 4 months ago

Hi,

Hope you are all well.

I want to create thing via android kotlin.

Kotlin Code:

    fun initAWSIotClient(context: Context) {
        val credentialsProvider = CognitoCachingCredentialsProvider(
            context,
            "MY_USER_POOL_ID",
            MY_REGION 
        )
        iotClient = AWSIotClient(credentialsProvider)

        createThing("MyNewThing")
    }

    private fun createThing(thingName: String) {
        Thread {
            try {
                val createThingRequest = CreateThingRequest()
                createThingRequest.thingName = thingName

                val createThingResult = iotClient.createThing(createThingRequest)
                app.l("Thing created: ${createThingResult.thingName}")
            } catch (e: Exception) {
                app.l("Error creating thing: $e")
            }

        }.start()
    }

However, I receive error:

Failure to get credentials

com.amazonaws.services.cognitoidentity.model.NotAuthorizedException: Unauthenticated access is not supported for this identity pool. (Service: AmazonCognitoIdentity; Status Code: 400; Error Code: NotAuthorizedException; Request ID: xxxxx)

I also added different permissions to the IAM role like AWSIoTFullAccess, AWSIoTThingsRegistration, and test custom policy:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "iot:CreateThing",
                "iot:DescribeThing",
                "iot:DeleteThing"
            ],
            "Resource": "*"
        }
    ]
}

I have also check Cognito -> user pool -> user access and the role has been applied there. However, the error persists. Do you know how I should solve the error?

dependencies:

    implementation 'com.amazonaws:aws-android-sdk-iot:2.75.2'
    implementation 'com.amazonaws:aws-android-sdk-mobile-client:2.75.2'

    implementation 'com.amplifyframework:core:2.16.0'
    implementation 'com.amplifyframework:aws-api:2.16.0'
    implementation 'com.amplifyframework:aws-datastore:2.16.0'

    implementation 'com.amazonaws:aws-android-sdk-ddb:2.73.0'
    implementation 'com.amazonaws:aws-android-sdk-mobile-client:2.75.2'
    implementation 'com.amazonaws:aws-android-sdk-auth-core:2.75.2'

    implementation 'com.amazonaws:aws-android-sdk-cognitoidentityprovider:2.75.2'

    implementation 'com.amplifyframework:aws-auth-cognito:2.16.0'
    implementation("io.github.jan-tennert.supabase:postgrest-kt:0.7.6")
    implementation("io.ktor:ktor-client-cio:2.3.3")

    implementation 'com.amplifyframework:aws-storage-s3:2.16.0'

    implementation("aws.sdk.kotlin:dynamodb:1.0.30")
    implementation("aws.sdk.kotlin:secretsmanager:1.0.30")
    implementation("aws.smithy.kotlin:http-client-engine-okhttp:1.0.11")
    implementation("aws.smithy.kotlin:http-client-engine-crt:0.30.0")

    implementation 'aws.sdk.kotlin:aws-core:1.0.44'

Thank you.

lawmicha commented 4 months ago

Hi @mbahmani90, did you enable "Allow unauthenticated access" in the Cognito Identity Pool? (not the Cognito User Pool options)

github-actions[bot] commented 3 months ago

This issue is now closed. Comments on closed issues are hard for our team to see. If you need more assistance, please open a new issue that references this one.