Это мой сценарий:
Я создал Мой пул пользователей и Мой пул удостоверений , затем я связался друг с другом вот так
После этого я следовал этому руководству (https://docs.aws.amazon.com/cognito/latest/developerguide/tutorial-integrating-user-pools-javascript.html)
var poolData = {
UserPoolId : 'us-east-2_{my_user_pool_id}',
ClientId : '{user_pool_client_id}'
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var userData = {
Username : 'user_name',
Pool : userPool
};
var authenticationData = {
Username : 'user_name', // your username here
Password : 'password', // your password here
};
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
var accessToken = result.getIdToken().getJwtToken();
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'us-east-2:{my_identity_pool_id}',
Logins: {
'cognito-idp.us-east-2.amazonaws.com/us-east-2_{my_user_pool_id}': accessToken
}
});
AWS.config.credentials.get(function(err){
if (err) {
alert(err);
}
window.mqttClient.updateWebSocketCredentials(AWS.config.credentials.AccessKeyId,
AWS.config.credentials.SecretKey,
AWS.config.credentials.SessionToken);
});
},
onFailure: function(err) {
alert(err);
},
mfaRequired: function(codeDeliveryDetails) {
var verificationCode = prompt('Please input verification code' ,'');
cognitoUser.sendMFACode(verificationCode, this);
}
});
const mqttClient = AWSIoTData.device({
region: AWS.config.region,
host: AWSConfiguration.host,
clientId: clientId,
protocol: 'wss',
maximumReconnectTimeMs: 8000,
debug: true,
accessKeyId: '',
secretKey: '',
sessionToken: ''
});
Я включил журнал CloudWatch и получил этот журнал:
2018-10-13 21:55:09.890 TRACEID:xxxx-xxx-xxxx-xxxx-xxxxxxxxx PRINCIPALID:XXXXXXXXXXX:CognitoIdentityCredentials [ERROR] EVENT:MQTT Client Connect MESSAGE:Connect Status: AUTHORIZATION_ERROR Failure reason:AUTHORIZATION_FAILURE
Почему я не могу получить доступ к aws-ресурсам через mqtt после того, как перехватил учетные данные в cognito AWS.config.credentials.get
?