【问题标题】:Android - How to you CognitoCachingCredentialsProvider for authenticated user?Android - 如何为经过身份验证的用户提供 CognitoCachingCredentialsProvider?
【发布时间】:2017-12-24 03:31:54
【问题描述】:

我需要以经过身份验证的用户身份将文件上传到 S3。我已经设法从 CognitoUserPool 作为 CognitoUser 获取令牌。

这里是代码。

String poolId = "xxxxxxxxxxx";
    String clientId = "xxxxxxxxxxx";
    String clientSecret = "xxxxxxxxxxxx";

// Create a CognitoUserPool object to refer to your user pool
CognitoUserPool userPool = new CognitoUserPool(getApplicationContext(), poolId, clientId, clientSecret,Regions.XX_XXXX_X);

CognitoUser user = userPool.getUser();

user.getSessionInBackground(authenticationHandler);

下面是 AuthenticationHandler 回调。

final AuthenticationHandler authenticationHandler = new 
AuthenticationHandler() {

        @Override
        public void onSuccess(CognitoUserSession cognitoUserSession, CognitoDevice cognitoDevice) {

            Log.d("success",cognitoUserSession.getAccessToken().getJWTToken());

        }

        @Override
        public void getAuthenticationDetails(AuthenticationContinuation authenticationContinuation, String userId) {
            // The API needs user sign-in credentials to continue
            AuthenticationDetails authenticationDetails = new AuthenticationDetails("test", "test", null);

            // Pass the user sign-in credentials to the continuation
            authenticationContinuation.setAuthenticationDetails(authenticationDetails);

            // Allow the sign-in to continue
            authenticationContinuation.continueTask();
        }

       ...


        @Override
        public void onFailure(Exception exception) {
            // Sign-in failed, check exception for the cause
            Log.d("Error here",exception.toString());
        }
    };

我应该自己将这些令牌保存在 SharedPereference 中吗?或者有没有其他办法。

提前致谢。

【问题讨论】:

  • 有人能解释一下我如何获得 clientSecret 吗?

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


【解决方案1】:

此处描述了将用户池用户与您的身份池相关联的过程:http://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-integrating-user-pools-with-identity-pools.html

cognitoUser.getSessionInBackground(new AuthenticationHandler() {
    @Override
    public void onSuccess(CognitoUserSession session) {
        String idToken = session.getIdToken().getJWTToken();

        Map<String, String> logins = new HashMap<String, String>();
        logins.put(cognito-idp.<region>.amazonaws.com/<YOUR_USER_POOL_ID>, session.getIdToken().getJWTToken());
        credentialsProvider.setLogins(logins);
        credentialsProvider.refresh();
    }

});

【讨论】:

  • 有人能解释一下我如何获得 clientSecret 吗?
  • 嗨@jeff 你能解释一下如何创建credentialsProvider 对象吗? credentialsProvider 对象的类型是什么。文档没有涵盖这些内容。
猜你喜欢
  • 2017-09-11
  • 2015-04-11
  • 2020-11-15
  • 1970-01-01
  • 2016-01-17
  • 1970-01-01
  • 2020-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多