我发布了here on the AWS forum请求中包含的安全令牌无效。 aws js sdk
我在下面的代码中使用了aws-js-sdk v2.2.3。我收到的数据与Credentials填充。当我尝试使用凭据时,我收到错误信息,证明它们无效。我正在使用开发人员身份验证流程。我有两个角色Auth & UnAuth。我的身份池看起来是正确的。信任关系看起来像是指向正确的身份池ID。 DynamoDB中存在针对S3 &的Auth角色的策略。我很茫然。任何帮助,将不胜感激。
JavaScript客户端:
var cognitoidentity = new AWS.CognitoIdentity({region: 'us-east-1'});
var params = {
IdentityId: user.cognito_id,
Logins: {
'cognito-identity.amazonaws.com': user.cognito_token
}
};
cognitoidentity.getCredentialsForIdentity(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data.Credentials);
});
我CONSOLE.LOG的Id &中SecretKey它们填充在
var aws_creds = StateService.get('user').aws_creds;
console.log(aws_creds.AccessKeyId);
console.log(aws_creds.SecretKey);
AWS.config.update({ accessKeyId: aws_creds.AccessKeyId,
secretAccessKey: aws_creds.SecretKey,
endpoint: ENV.aws_dyndb_endpoint,
region: 'us-east-1'
});
var dynamodb = new AWS.DynamoDB();
console.log("user obj: ", StateService.get('user'));
var params = {
TableName: games_table_name,
KeyConditionExpression: "Id = :v1",
ExpressionAttributeValues: {
":v1": {"N": id}
}
};
return dynamodb.query(params);
我的解决方案
我想出什么样的主意是明确刷新凭证,而不是在我为实例创建DynamoDb对象时懒惰地获取凭证。这是我使用的函数,当刷新凭证时,返回承诺&。
refresh: function() {
var deferred = $q.defer();
AWS.config.region = 'us-east-1';
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: COGNITO_IDENTITY_POOL_ID,
IdentityId: COGNITO_ID,
Logins: 'cognito-identity.amazonaws.com'
});
AWS.config.credentials.refresh(function(error) {
if ((error === undefined) || (error === null)) {
$log.debug("Credentials Refreshed Success: ", AWS.config.credentials);
var params = {
region: 'us-east-1',
apiVersion: '2012-08-10',
credentials: AWS.config.credentials
};
$rootScope.dynamodb = new AWS.DynamoDB({params: params});
deferred.resolve();
}
else {
$log.debug("Error refreshing AWS Creds:, ", error);
deferred.reject(error);
}
});
return deferred.promise;
}
保存getCredentialsForIdentity()响应的代码在哪里?在失败的操作中实际使用这些凭据的代码在哪里? – jarmod