2017-07-19 14 views
0

我需要上傳文件到S3作爲認證用戶。我設法從CognitoUserPool獲得CognitoUser的令牌。Android - 如何爲認證用戶提供CognitoCachingCredentialsProvider?

這是代碼。

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這些令牌?或者還有其他方法。

在此先感謝。

回答

0

這裏描述爲以您的身份游泳池的用戶池用戶鏈接的過程: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(); 
    } 

});

相關問題