登录名不匹配。请为此身份或身份池至少包含一个有效的登录名

时间:2019-02-04 10:11:32

标签: android amazon-web-services amazon-s3 amazon-cognito

我正在尝试将登录凭据(例如auth0登录的访问令牌)放置到Amazon Cognito,然后尝试在我的应用中上传个人资料图片。我从亚马逊收到错误消息,例如:

Logins don't match. Please include at least one valid login for this identity or identity pool. (Service: AmazonCognitoIdentity; Status Code: 400; Error Code: NotAuthorizedException; Request ID: 0fafe672-2863-11e9-8f0e-cdc8b612afa1)

UploadImageTask.java

private class UploadImageTask extends AsyncTask<Void, Demo, Demo> {
        final ConditionVariable conditionVariable = new ConditionVariable(false);
        Demo demo = new Demo();
        File compressImage;
        private User user;

        UploadImageTask(User user) {
            this.user = user;
        }

        @Override
        protected Demo doInBackground(Void... params) {
            compressImage = new File(compressImage(user.getProfileUrl()));
            String uniqueId = UUID.randomUUID().toString();
            final String keyName = "user/" + uniqueId + Global.IMAGE_JPG_EXTENSION;
            CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                    SettingsAccountActivity.this,
                    BuildConfig.AMAZON_POOL_ID,
                    Regions.fromName(BuildConfig.AMAZON_REGION));
            ClientConfiguration configuration = new ClientConfiguration();
            SessionManager sessionManager = new SessionManager(SettingsAccountActivity.this);
            String accessToken = sessionManager.getAuthToken().getAccess_Token();
            Map<String, String> logins = new HashMap<>();
            logins.put("myname.auth0.com", "Bearer "+ accessToken);
            AppLogger.d(getTag(), logins.toString());
            credentialsProvider.setLogins(logins);
            configuration.setMaxErrorRetry(3);
            configuration.setConnectionTimeout(501000);
            configuration.setSocketTimeout(501000);
            configuration.setProtocol(Protocol.HTTP);
            AmazonS3Client s3Client = new AmazonS3Client(credentialsProvider, configuration);
            if (BuildConfig.FLAVOR == Global.FLAVOR_PRO) {
                s3Client.setRegion(Region.getRegion(Regions.fromName("us-east-2")));
            }
            TransferUtility transferUtility = new TransferUtility(s3Client, SettingsAccountActivity.this);
            ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
            TransferObserver observer = transferUtility.upload(BuildConfig.BUCKET_NAME, keyName,
                    compressImage, objectMetadata);
            observer.setTransferListener(new TransferListener() {
                @Override
                public void onStateChanged(int id, TransferState state) {
                    if (state == TransferState.COMPLETED) {
                        user.setProfileUrl(keyName);
                        demo.setShow(true);
                        demo.setKeyName(keyName);
                        FutureTarget<File> futureTarget = Glide.with(getApplicationContext())
                                .load(BuildConfig.AMAZON_URL + BuildConfig.BUCKET_NAME + "/" + keyName)
                                .downloadOnly(1024, 1024);
                        new Thread(() -> {
                            try {
                                File file = futureTarget.get();
                                String path = file.getAbsolutePath();
                                AppLogger.d(getTag(), "----------------- cache Image: " + path);
                            } catch (InterruptedException | ExecutionException e) {
                                AppLogger.e(getTag(), e.getMessage(), e);
                            }
                            conditionVariable.open();
                        }).start();
                    }
                }


                @Override
                public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
                    AppLogger.d(getTag(), "--- is Uploading");
                }

                @Override
                public void onError(int id, Exception ex) {
                    AppLogger.e(getTag(), ex.getMessage(), ex);
                    conditionVariable.open();
                    demo.setKeyName(keyName);
                    demo.setShow(false);
                }
            });
            conditionVariable.block();
            return demo;
        }

        @Override
        protected void onPostExecute(Demo demo) {
            super.onPostExecute(demo);
            if (compressImage.exists() && compressImage.delete()) {
                AppLogger.d(getTag(), "Compress image deleted");
            }
            if (demo.isShow()) {
                String keyName = demo.getKeyName();
                AppLogger.d("Key name is:", keyName);
                if (user.getProfileUrl().startsWith(Global.FIELD_HTTP) || user.getProfileUrl().startsWith(Global.FIELD_HTTPS)) {
                    url = user.getProfileUrl();
                    user.setProfileUrl(url);
                } else {
                    url = BuildConfig.AMAZON_URL + BuildConfig.BUCKET_NAME + "/" + user.getProfileUrl();
                    user.setProfileUrl(url);
                    AppLogger.e("AMAZON URL:::", "url is:" + url);
                }
                Glide.with(getApplicationContext())
                        .load(url)
                        .transform(new GlideCircleTransform(getApplicationContext()))
                        .override(100, 200)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .listener(new RequestListener<String, GlideDrawable>() {
                            @Override
                            public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                                mProgressView.setVisibility(View.GONE);
                                return false;
                            }

                            @Override
                            public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                                mProgressView.setVisibility(View.GONE);
                                return false;
                            }
                        })
                        .into(userAccountImage);
                SessionManager manager = new SessionManager(SettingsAccountActivity.this);
                manager.getAuthToken().getUser().setProfileUrl(url);
                AuthToken token = manager.getAuthToken();
                manager.startSession(token);
                ApiClient apiClient = new ApiClient(SettingsAccountActivity.this);
                apiClient.changeProfile(keyName, imageApiCallback);

            } else {
                showProgress(false);
                mProgressView.setVisibility(View.GONE);
                //Toast.makeText(SettingsAccountActivity.this, getResources().getString(R.string.string_java_toast_error_in_adding_task), Toast.LENGTH_SHORT).show();
            }
        }
    }

我正在执行此操作以上传图像,并尝试将accesstoken放入Amazon Cognito。但是我遇到错误,图片没有上传。

ErrrLog

2019-02-04 15:25:53.776 23419-25377/com.visualogyx.app.dev E/CognitoCachingCredentialsProvider: Failure to get credentials
    com.amazonaws.services.cognitoidentity.model.NotAuthorizedException: Logins don't match. Please include at least one valid login for this identity or identity pool. (Service: AmazonCognitoIdentity; Status Code: 400; Error Code: NotAuthorizedException; Request ID: 0fafe672-2863-11e9-8f0e-cdc8b612afa1)
        at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:729)
        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:559)
        at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.getId(AmazonCognitoIdentityClient.java:445)
        at com.amazonaws.auth.AWSAbstractCognitoIdentityProvider.getIdentityId(AWSAbstractCognitoIdentityProvider.java:172)
        at com.amazonaws.auth.AWSEnhancedCognitoIdentityProvider.refresh(AWSEnhancedCognitoIdentityProvider.java:76)
        at com.amazonaws.auth.CognitoCredentialsProvider.startSession(CognitoCredentialsProvider.java:601)
        at com.amazonaws.auth.CognitoCredentialsProvider.getCredentials(CognitoCredentialsProvider.java:388)
        at com.amazonaws.auth.CognitoCachingCredentialsProvider.getCredentials(CognitoCachingCredentialsProvider.java:442)
        at com.amazonaws.auth.CognitoCachingCredentialsProvider.getCredentials(CognitoCachingCredentialsProvider.java:76)
        at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4619)
        at com.amazonaws.services.s3.AmazonS3Client.getBucketRegionViaHeadRequest(AmazonS3Client.java:5377)
        at com.amazonaws.services.s3.AmazonS3Client.fetchRegionFromCache(AmazonS3Client.java:5352)
        at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4616)
        at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1728)
        at com.amazonaws.mobileconnectors.s3.transferutility.UploadTask.uploadSinglePartAndWaitForCompletion(UploadTask.java:214)
        at com.amazonaws.mobileconnectors.s3.transferutility.UploadTask.call(UploadTask.java:88)
        at com.amazonaws.mobileconnectors.s3.transferutility.UploadTask.call(UploadTask.java:47)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
        at java.lang.Thread.run(Thread.java:761)

有人可以帮助我解决此问题吗? 我已经访问过亚马逊论坛ans并尝试以下操作: https://forums.aws.amazon.com/thread.jspa?threadID=243850,但无效。

谢谢。

1 个答案:

答案 0 :(得分:1)

这应该是配置问题。作为解决方案,您必须从头开始创建一个新的身份池,并在创建过程中添加新的身份池,将cognito用户池作为身份验证提供程序。

如果上述解决方案无效,则workaround

one more solution