我正在开发一个应用程序,我需要跟踪某些设备的更改并在前端显示这些更改。
对于用户登录,我使用的是 cognito,登录后我获得了凭证,并且我已经获得了有效的凭证,因为我使用相同的凭证连接了 AWS DynamoDB。
现在我想使用相同的认知凭证注册 aws.iot 设备。
我正在关注https://github.com/aws/aws-iot-device-sdk-js
我用 aws 用户检查了一些静态凭证,例如:
client.device = awsIot.device({
clientId: clientID,
host: host,
accessKeyId: AccessKeyId,
secretKey: secretKey,
protocol: 'wss'
});
这工作得很好。
然后我使用 aws cognitoassessKeyId 和 SecretKey 尝试了相同的操作,但这次我得到了 403。
我检查了connect to AWS IoT using web socket with Cognito authenticated users ,但没有帮助。
我当前的代码如下:
var awsIot = require('aws-iot-device-sdk');
AWS.config.credentials.get(() => {
const IoT = new AWS.Iot();
IoT.attachPrincipalPolicy({
policyName: 'PubSub',
principal: AWS.config.credentials.identityId
}, (err, res) => {
if (err) {
} else {
let credential;
if (AWS.config.credentials && AWS.config.credentials.data && AWS.config.credentials.data.Credentials) {
let credentials = AWS.config.credentials.data.Credentials;
awsIot.device({
clientId: clientID,
host: host,
accessKeyId: credentials.AccessKeyId,
secretKey: credentials.secretKey,
protocol: 'wss',
sessionToken: credentials.SessionToken
});
}
}
});
});
任何人都可以帮助我,我在这里缺少什么。
最佳答案
对我有用的是直接从 AWS.config.credentials 对象传递数据,即
if (AWS.config.credentials) {
awsIot.device({
clientId: clientID,
host: host,
accessKeyId: AWS.config.credentials.accessKeyId,
secretKey: AWS.config.credentials.secretAccessKey,
protocol: 'wss',
sessionToken: AWS.config.credentials.sessionToken
});
}
如果您通过此方法调用,也许还要检查 accessKeyId 等是否以小写字母开头而不是大写字母。
关于aws-sdk - 使用 Cognito 凭证注册 aws IoT 设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47433960/