在android

时间:2016-12-01 12:35:12

标签: android aws-sdk amazon-cognito

我在尝试使用Cognito Identity同步用户池登录时尝试解决此问题。

以下代码片段使用具有Cognito身份的整合用户池

private AuthenticationHandler authenticationHandler = new AuthenticationHandler() {

    @Override
    public void onSuccess(CognitoUserSession userSession, CognitoDevice device) {   
        //Sync User pool login in Cognito 
        syncCognitoLogin(userSession);

    }

    @Override
    public void onFailure(Exception e) {
        Log.d("AlexaCognitoLoginFragment", "onFailure error : " + e.getMessage());
    }

    @Override
    public void getMFACode(MultiFactorAuthenticationContinuation arg0) {

    }

    @Override
    public void getAuthenticationDetails(AuthenticationContinuation continuation,
            String userName) {

        //set authentication details
        AuthenticationDetails authenticationDetails = new AuthenticationDetails(userName, "12345678", null);
        continuation.setAuthenticationDetails(authenticationDetails);
        continuation.continueTask();

    }

    @Override
    public void authenticationChallenge(ChallengeContinuation arg0) {

    }
};

private void syncCognitoLogin(final CognitoUserSession session){
     if(cognitoSyncManager == null){
         throw new IllegalStateException("Sync Manager not yet initialized");
     }


     new AsyncTask<Void, Void, Void>() {

        @Override
        protected Void doInBackground(Void... params) {

             Map<String, String> logins = cachingCredentialsProvider.getLogins();

             if(logins == null) {
                 logins = new HashMap<String, String>();
             }
             logins.put("cognito-idp." + Regions.US_EAST_1 + ".amazonaws.com/" + USER_POOL_ID, session.getIdToken().getJWTToken());
             cachingCredentialsProvider.setLogins(logins);

            String identityID = cachingCredentialsProvider.getIdentityId();
            Log.i(TAG, "Identity ID=" + identityID );

            return null;
        }
    }.execute();

    Log.i(TAG, " Cognito Login sync successfully for session " + session.getIdToken().getJWTToken());


}

尝试登录时出错

  

E / CognitoCachingCredentialsProvider:无法获取凭据   E / CognitoCachingCredentialsProvider:com.amazonaws.services.cognitoidentity.model.NotAuthorizedException:此标识池不支持未经身份验证的访问。 (服务:AmazonCognitoIdentity;状态代码:400;错误代码:NotAuthorizedException;请求ID:96dc9b9f-b7c1-11e6-9f6d-b3b036ebf640)   E / CognitoCachingCredentialsProvider:at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:712)   E / CognitoCachingCredentialsProvider:at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:388)   E / CognitoCachingCredentialsProvider:at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:199)   E / CognitoCachingCredentialsProvider:at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.invoke(AmazonCognitoIdentityClient.java:558)   E / CognitoCachingCredentialsProvider:at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.getId(AmazonCognitoIdentityClient.java:444)   E / CognitoCachingCredentialsProvider:at com.amazonaws.auth.AWSAbstractCognitoIdentityProvider.getIdentityId(AWSAbstractCognitoIdentityProvider.java:172)   E / CognitoCachingCredentialsProvider:at com.amazonaws.auth.AWSEnhancedCognitoIdentityProvider.refresh(AWSEnhancedCognitoIdentityProvider.java:76)   E / CognitoCachingCredentialsProvider:at com.amazonaws.auth.CognitoCredentialsProvider.startSession(CognitoCredentialsProvider.java:561)   E / CognitoCachingCredentialsProvider:at com.amazonaws.auth.CognitoCredentialsProvider.getCredentials(CognitoCredentialsProvider.java:371)   E / CognitoCachingCredentialsProvider:at com.amazonaws.auth.CognitoCachingCredentialsProvider.getCredentials(CognitoCachingCredentialsProvider.java:441)   E / CognitoCachingCredentialsProvider:at com.amazonaws.auth.CognitoCachingCredentialsProvider.getCredentials(CognitoCachingCredentialsProvider.java:76)   E / CognitoCachingCredentialsProvider:at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.invoke(AmazonDynamoDBClient.java:4168)   E / CognitoCachingCredentialsProvider:at com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient.getItem(AmazonDynamoDBClient.java:1232)   E / CognitoCachingCredentialsProvider:at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.load(DynamoDBMapper.java:393)   E / CognitoCachingCredentialsProvider:at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.load(DynamoDBMapper.java:466)   E / CognitoCachingCredentialsProvider:at com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMapper.load(DynamoDBMapper.java:340)   E / CognitoCachingCredentialsProvider:at com.amazonaws.youruserpools.UserActivity $ 2.doInBackground(UserActivity.java:256)   E / CognitoCachingCredentialsProvider:at com.amazonaws.youruserpools.UserActivity $ 2.doInBackground(UserActivity.java:252)   E / CognitoCachingCredentialsProvider:在android.os.AsyncTask $ 2.call(AsyncTask.java:288)   E / CognitoCachingCredentialsProvider:at java.util.concurrent.FutureTask.run(FutureTask.java:237)   E / CognitoCachingCredentialsProvider:在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)   E / CognitoCachingCredentialsProvider:at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)   E / CognitoCachingCredentialsProvider:at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587)   E / CognitoCachingCredentialsProvider:at java.lang.Thread.run(Thread.java:841)

2 个答案:

答案 0 :(得分:0)

您看到的错误表明您尝试以未经身份验证的方式访问联合身份(表示未提供身份验证凭据),并且您的身份池不支持未经身份验证的提供程序。

您需要使用用户名和版本对用户进行身份验证密码和获取id标记作为响应。这将在登录映射中用于在凭证提供程序上设置。

我们有integration of user pools with federated identities的详细文档。如果您在遵循文档时遇到任何问题,请告诉我们。

答案 1 :(得分:0)

错误显示Unauthenticated access

试试这个。转到Federated Identities&gt; Edit Identity Pool

然后尝试通过单击“创建新角色”添加“未经身份验证的角色”。 (如果未经身份验证的角色中存在任何角色,请不要更改)。 enter image description here 在下一页中,单击右下方的“允许”。

如果问题仍然存在,请执行以下任一操作:

  1. 如果您使用过Mobile Hub并对用户登录设置&gt;进行了更改用户是否需要登录您的应用? Make是可选项,然后使用示例代码
  2. 否则,请检查AWSConfiguration文件,该文件存储所有必需的ID(标识池ID,客户端ID)并验证它们与您正在使用的ID匹配。
  3. 有关文档和详细信息,请参阅Rachit's Comment