我使用从 Cognito 的亚马逊开放 ID token 兑换的凭证时收到访问 key 无效
错误
这就是我正在做的事情
获取开发者验证的开放 ID token
cognito.getOpenIdTokenForDeveloperIdentity(参数,函数(错误,数据){
openIdToken = data.credentials });
将开放 ID token 兑换为安全凭证,我将参数设置为 congnito Auth 角色并设置任意角色 session 名称。我使用步骤 1 中的 token 。没有地方可以设置步骤 1 中的身份 ID。
it('should be able to exchange temporary open id token for auth credentials', function (done) { var sts = new AWS.STS(); var params = { RoleArn: roleArn, RoleSessionName: 'photo-upload-session', WebIdentityToken: openIdToken.Token }; sts.assumeRoleWithWebIdentity(params, function(err, data) { should.not.exist(err); should.exist(data.Credentials.AccessKeyId); should.exist(data.Credentials.SecretAccessKey); should.exist(data.Credentials.SessionToken); credentials = data.Credentials; done(); }); });
我更新当前凭据
AWS.config.update({accessKeyId:credentials.AccessKeyId,secretAccessKey:credentials.SecretAccessKey});
我将文件上传到 s3 并收到
[InvalidAccessKeyId:您提供的 AWS 访问 key ID 在我们的记录中不存在。]
错误
*使用 Bob Kinney 的建议编辑我尝试了两种方法 - 设置 sessionToken (有效)和使用 Congito 凭据,这给出了 TypeError 而不是缓冲区错误。 CognitoIdentityCredentials 示例如下。
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId:config.get('aws_identity_pool_id'),
Logins: {
'cognito-identity.amazonaws.com': openIdToken.Token
}
});
var body = fs.createReadStream(__dirname + '/test_photo.jpg');
var s3obj = new AWS.S3({params: {Bucket: 'test-uploads', Key: 'test'}});
s3obj.upload({Body: body}).
on('httpUploadProgress', function(evt) { console.log(evt); }).
send(function(err, data) {
should.not.exist(err);
done();
});
** 更新
因此,回到 java 客户端错误,我们使用 openid token (经过测试,该 token 可以与 sts.assumeRoleWithWebIdentity 一起正常工作)并将该 token 传递到 AWSAbstractCognitoIdentityProvider 的扩展中(代码取自此链接 http://docs.aws.amazon.com/cognito/devguide/identity/developer-authenticated-identities/ ) - 然后使用该身份上传到 s3 并出现错误
CustomAwsIdentityProvider provider = CustomAwsIdentityProvider.newInstance(this, BuildConfig.AWS_COGNITO_POOL_ID, Regions.US_EAST_1);
CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(this, provider, Regions.US_EAST_1);
TransferManager tm = new TransferManager(credentialsProvider);
tm.upload("my-upload", uuid.toString(), file);
最佳答案
对于出现的问题,我们深表歉意。您似乎正在使用 JavaScript SDK。使用此流程时,您可以使用 developer guide 中提到的标准 AWS.CognitoIdentityCredentials
对象。使用 cognito-identity.amazonaws.com
的 key 以及从 getOpenIdTokenForDeveloperIdentity
调用返回的 OpenId Connect token 值。
您看到错误的原因是您没有包含 STS 结果中的 sessionToken。使用 AWS.CognitoIdentityCredentials
对象应该可以解决此问题。
更新 2015-07-21: SDK 中存在一个小问题,不幸的是,该问题会阻止 AWS.CognitoIdentityCredentials
按照我所描述的方式工作。我们正在努力缓解这个问题。
更新 2015 年 7 月 24 日:您应该能够通过以下方式将 AWS.CognitoIdentityCredentials
与您经过开发人员身份验证的身份结合使用:
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: 'MY_IDENTITY_POOL',
IdentityId: data.IdentityId,
Logins: {
'cognito-identity.amazonaws.com': data.Token
}
});
其中数据是来自 GetOpenIdTokenForDeveloperIdentity
的响应。
关于amazon-web-services - 使用从亚马逊开放 ID token 兑换的凭据时出现无效的访问 key 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31521491/