在Android中使用以下代码时,无法从链中任何提供商加载AWS凭证

时间:2018-07-26 12:35:12

标签: amazon-web-services amazon-cognito

字符串cognitoIdentityId = sp.getString(“ aws_identity”,“”);                 字符串openIdToken = sp.getString(“ aws_token”,“”);

            Map<String,String> logins = new HashMap<>();
            logins.put("cognito-identity.amazonaws.com", openIdToken);
            GetCredentialsForIdentityRequest getCredentialsRequest =
                    new GetCredentialsForIdentityRequest()
                            .withIdentityId(cognitoIdentityId)
                            .withLogins(logins);
            AmazonCognitoIdentityClient cognitoIdentityClient = new AmazonCognitoIdentityClient();
            GetCredentialsForIdentityResult getCredentialsResult = cognitoIdentityClient.getCredentialsForIdentity(getCredentialsRequest);
            Credentials credentials = getCredentialsResult.getCredentials();
            AWSSessionCredentials sessionCredentials = new BasicSessionCredentials(
                    credentials.getAccessKeyId(),
                    credentials.getSecretKey(),
                    credentials.getSessionToken()
            );

1 个答案:

答案 0 :(得分:0)

错误在于语法 正确的代码是

cognitoIdentityId = sp.getString("aws_identity", "");
            String openIdToken = sp.getString("aws_token", "");

            Map<String, String> logins = new HashMap<>();
            logins.put("cognito-identity.amazonaws.com", openIdToken);
            GetCredentialsForIdentityRequest getCredentialsRequest =
                    new GetCredentialsForIdentityRequest()
                            .withIdentityId(cognitoIdentityId)
                            .withLogins(logins);
            AmazonCognitoIdentityClient cognitoIdentityClient = new AmazonCognitoIdentityClient(new AnonymousAWSCredentials());
            GetCredentialsForIdentityResult getCredentialsResult = cognitoIdentityClient.getCredentialsForIdentity(getCredentialsRequest);
            Credentials credentials = getCredentialsResult.getCredentials();
            AWSSessionCredentials sessionCredentials = new BasicSessionCredentials(
                    credentials.getAccessKeyId(),
                    credentials.getSecretKey(),
                    credentials.getSessionToken()
            );

            AWS_KEY = sessionCredentials.getAWSAccessKeyId();
            AWS_SECRET = sessionCredentials.getAWSSecretKey();
            AWS_SESSION = sessionCredentials.getSessionToken();