我需要将文件作为经过身份验证的用户上传到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中保存这些令牌吗?或者还有其他方法。
提前致谢。
答案 0 :(得分: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();
}
});