从Cognito用户池

时间:2018-04-01 09:50:26

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

我尝试使用以下代码从AWS的cognito用户池中获取用户的属性(email-id),但它似乎不起作用。如何做到这一点以及我的代码中的问题?

AWSMobileClient.getInstance().initialize(this, new AWSStartupHandler() {
    public DynamoDBMapper dynamoDBMapper;

    @Override
    public void onComplete(AWSStartupResult awsStartupResult) {
        PinpointConfiguration pinpointConfig = new PinpointConfiguration(
                getApplicationContext(),
                AWSMobileClient.getInstance().getCredentialsProvider(),
                AWSMobileClient.getInstance().getConfiguration());
        pinpointManager = new PinpointManager(pinpointConfig);

        // Start a session with Pinpoint
        pinpointManager.getSessionClient().startSession();

        AmazonDynamoDBClient dynamoDBClient = new AmazonDynamoDBClient(AWSMobileClient.getInstance().getCredentialsProvider());
        this.dynamoDBMapper = DynamoDBMapper.builder().dynamoDBClient(dynamoDBClient).awsConfiguration(AWSMobileClient.getInstance().getConfiguration()).build();
        identityManager=new IdentityManager(getApplicationContext(),AWSMobileClient.getInstance().getConfiguration());
        CognitoUserPool userPool = new CognitoUserPool(getApplicationContext(),"xx-xxxxx-xxxx-xx","xxxxxxxxxxxxxxxxx","xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");

        CognitoUser user = userPool.getUser(identityManager.getCachedUserID());
        GetDetailsHandler handler = new GetDetailsHandler() {
            @Override
            public void onSuccess(CognitoUserDetails cognitoUserDetails) {
                Map user = cognitoUserDetails.getAttributes().getAttributes();
                Collection<Object> keys = user.keySet();
                for (Object entry:keys
                     ) {
                    Log.d("key",entry.toString());
                }
            }

            @Override
            public void onFailure(Exception exception) {
                Log.d("details","fail");
            }
        };
        user.getDetails(handler);

1 个答案:

答案 0 :(得分:1)

尝试使用entrySet()并确保userPool具有(email-id)属性

    @Override
    public void onSuccess(CognitoUserDetails cognitoUserDetails) {
        Map<String, String> user = cognitoUserDetails.getAttributes().getAttributes();
        for (Map.Entry<String, String> entry : user.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            Log.d("key", "key: " + key);
            Log.d("key", "value: " + value);
            settings.edit().putString(key, value).apply();
        }
    }