Aws-sdk-android: Getting CognitoCachingCredentialsProvider: Failure to get credentials

Created on 27 Feb 2019  路  3Comments  路  Source: aws-amplify/aws-sdk-android

Describe the bug
I am using CognitoUserpool for creating user and signin.
Now i am uploading image to s3 bucket. Issue is while i am trying to retrieve image from s3 getting below error
CognitoCachingCredentialsProvider: Failure to get credentials

To Reproduce
A code sample or steps:

   private fun initdata() {
        AppHelper.init(me)
        val credentialsProvider: AWSCredentialsProvider = AppHelper.getCredentialProvider()
        /*
        * Get images from amazon s3 bucket*/
        amazonS3 = AmazonS3Client(credentialsProvider)

        getObjectListData().execute()
    }
    inner class getObjectListData : AsyncTask<Void, Void, Void>() {

        override fun onPreExecute() {
            super.onPreExecute()
            showprogress()
        }

        override fun doInBackground(vararg params: Void?): Void? {
            //     observers = transferUtility?.getTransfersWithType(TransferType.DOWNLOAD)
            //   val listener: TransferListener = downloadlistener()

            val objectListing = amazonS3?.listObjects(bucket)
            //   val userObjectListing: MutableList<S3ObjectSummary> = arrayListOf()
            val summaries: MutableList<S3ObjectSummary>? = objectListing?.objectSummaries
            item?.clear()

            val uname = me.mPrefManager?.getUserName()!!
            for (summary in summaries!!) {
                if (summary.key.startsWith(uname)) {
                    //userObjectListing.add(summary)
                    model = MyselfModel()
                    Log.e("Summary", summary.key)

                    val line = "https://" + bucket + ".s3.us-east-2.amazonaws.com/" + summary.key
                    val url = URL(line)
//                    val presignedurl = GeneratePresignedUrlRequest(bucket, summary.key)
//                    val url: URL? = amazonS3?.generatePresignedUrl(presignedurl)
                    model.imgurl = url
                    item?.add(model)
                    Log.e("URL", url.toString())
                }

            }




            return null
        }

        override fun onPostExecute(result: Void?) {
            super.onPostExecute(result)
            hideprogress()

            val stagger = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)

            val spannedGridLayoutManager = SpannedGridLayoutManager(
                orientation = SpannedGridLayoutManager.Orientation.VERTICAL,
                spans = 4
            )

            val flexBoxLayoutManager = FlexboxLayoutManager(context)
                .apply {
                    flexWrap = FlexWrap.WRAP
                    flexDirection = FlexDirection.ROW
                    alignItems = AlignItems.STRETCH
                }

            binding.mySelfRecycler.layoutManager = spannedGridLayoutManager




            binding.mySelfRecycler.adapter = MyAdapter(context!!, item!!)

        }
    }

Which AWS service(s) are affected?

Expected behavior
Retrieve image from s3 bucket

Screenshots
If applicable, add screenshots to help explain your problem.

Environment Information (please complete the following information):

Additional context
Error Logs
2019-02-27 14:38:43.718 1886-1916/com.htispl.picapp E/CognitoCachingCredentialsProvider: Failure to get credentials
com.amazonaws.services.cognitoidentity.model.NotAuthorizedException: Access to Identity 'us-east-2:643f81c1-075f-4cf4-a4b1-f86da60fa3e6' is forbidden. (Service: AmazonCognitoIdentity; Status Code: 400; Error Code: NotAuthorizedException; Request ID: 4979a3d3-3a6f-11e9-914c-59dd6b0c1fb1)
at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:730)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:405)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:212)
at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.invoke(AmazonCognitoIdentityClient.java:1477)
at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.getCredentialsForIdentity(AmazonCognitoIdentityClient.java:698)
at com.amazonaws.auth.CognitoCredentialsProvider.populateCredentialsWithCognito(CognitoCredentialsProvider.java:782)
at com.amazonaws.auth.CognitoCredentialsProvider.startSession(CognitoCredentialsProvider.java:694)
at com.amazonaws.auth.CognitoCredentialsProvider.getCredentials(CognitoCredentialsProvider.java:465)
at com.amazonaws.auth.CognitoCachingCredentialsProvider.getCredentials(CognitoCachingCredentialsProvider.java:485)
at com.amazonaws.auth.CognitoCachingCredentialsProvider.getCredentials(CognitoCachingCredentialsProvider.java:77)
at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4649)
at com.amazonaws.services.s3.AmazonS3Client.getBucketRegionViaHeadRequest(AmazonS3Client.java:5424)
at com.amazonaws.services.s3.AmazonS3Client.fetchRegionFromCache(AmazonS3Client.java:5399)
at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4646)
at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4603)
at com.amazonaws.services.s3.AmazonS3Client.listObjects(AmazonS3Client.java:668)
at com.amazonaws.services.s3.AmazonS3Client.listObjects(AmazonS3Client.java:618)
at com.htispl.picapp.home.MySelfFragment$getObjectListData.doInBackground(MySelfFragment.kt:179)
at com.htispl.picapp.home.MySelfFragment$getObjectListData.doInBackground(MySelfFragment.kt:168)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)

Needs Info from Requester Usage Question

Most helpful comment

Thanks
Issue has been solved using AWSMobileClient

All 3 comments

@shabana0508 Thanks for opening the issue. From the error message it looks like either the authenticated role is not set up correctly or token from identity provider are not being passed via logins map. Please verify the above two cases.
For authentication solutions we strongly recommend that you use AWSMobileClient since it does a lot of heavy-lifting for you. You can initialize it as follows:

AWSMobileClient.getInstance().initialize(getApplicationContext(), new Callback<UserStateDetails>() {

        @Override
        public void onResult(UserStateDetails userStateDetails) {
            Log.i("INIT", "onResult: " + userStateDetails.getUserState());
        }

        @Override
        public void onError(Exception e) {
            Log.e("INIT", "Initialization error.", e);
        }
    }
);

Once initialized, you can use the AWSMobileClient instance as a credentials provider where ever needed.

Thanks
Issue has been solved using AWSMobileClient

Great. I will go ahead and close this issue. Please feel free to open another one if you have further questions.

Was this page helpful?
0 / 5 - 0 ratings