我无法获取我的CognitoIdentity的凭据。当用户成功认证后,他需要获取身份才能访问其他AWS服务。在我的情况下,它是AWS IoT。但不知何故,我无法获得任何凭据。
以下是错误信息:
“检索凭据时出错:NotAuthorizedException:禁止访问标识'eu-central-1:XXXXXXXXXX'。”
我的代码几乎与Github上的教程完全相同。
请注意,我跳过了不相关的代码。经过身份验证的用户可以完全访问我正在使用的所有AWS服务。
以下是错误信息:
“检索凭据时出错:NotAuthorizedException:禁止访问标识'eu-central-1:XXXXXXXXXX'。”
我的代码几乎与Github上的教程完全相同。
var cognitoUser = new AWSCognito.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
console.log("Logged in");
console.log('access token + ' + result.getAccessToken().getJwtToken());
// window.location.href = "index.html";
AWS.config.region = AWSConfiguration.region;
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: AWSConfiguration.IdPoolId,
Logins : {
'cognito-idp.eu-central-1.amazonaws.com/eu-central-1_XXXX' : result.getIdToken().getJwtToken()
}
});
var cognitoIdentity = new AWS.CognitoIdentity();
AWS.config.credentials.get(function(err, data) {
if (!err) {
console.log('retrieved identity: ' + AWS.config.credentials.identityId);
var params = {
IdentityId: AWS.config.credentials.identityId
};
cognitoIdentity.getCredentialsForIdentity(params, function(err, data) {
if (!err) {
thingShadows.updateWebSocketCredentials(data.credentials.AccessKeyId,
data.credentials.SecretKey,
data.credentials.SessionToken);
}
else {
console.log('error retrieving credentials: ' + err);
}
});
}
else {
console.log('error retrieving identity:' + err);
}
});
}
});
请注意,我跳过了不相关的代码。经过身份验证的用户可以完全访问我正在使用的所有AWS服务。