我've been trying to get Google Sign-In to work with Cognito through the JS SDK. I'一直在阅读文档:http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityCredentials.html
下面的代码在我的函数中,一旦我收到来自Google的回调就会运行 . 我已经确认我已通过Google登录并拥有access_token . 根据文档我应该只需要角色名称,如果角色是通过cognito配置的,那么它可以自动找出池ID . 它是什么 . 该函数调用get并成功返回 .
现在我不明白的是两件事:
1)一旦调用get方法,我不应该在Cognito的池中看到新的身份吗?我没有通过cognito看到任何登录 . 我应该何时在仪表板上看到通过cognito登录?
2)现在尝试使用AWS来访问特定资源,例如可能读取S3存储桶或因Auth而失败的事情,如果问题#1出现问题,这将是有意义的 .
我猜我错过了一些简单的东西,因为,任何帮助将不胜感激 . 我也查看了这个链接:http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/browser-configuring.html并尝试使用WebIdentityToken,但没有成功 .
AWS.config.region = 'us-east-1';
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: '<POOL ID>',
Logins: {
'accounts.google.com': googleUser.wc.access_token
}
});
// Obtain AWS credentials
AWS.config.credentials.get(function(){
// Credentials will be available when this function is called.
var accessKeyId = AWS.config.credentials.accessKeyId;
var secretAccessKey = AWS.config.credentials.secretAccessKey;
var sessionToken = AWS.config.credentials.sessionToken;
console.log("accessKeyId: " + accessKeyId);
});
3 回答
所以问题归结为两件事 .
就像Scott说我需要在CognitoIdentityCredentials方法中使用IdentityPoolId和Logins一样 .
第二个是来自谷歌的令牌 . 它不是AWS需要的访问令牌,它是id_token!所以在google成功回调中我们得到了googleUser对象,其上有一个wc对象,其中包含一个id_token . 您在登录词典上使用该令牌,您自己成功登录!
确保在AWS-> Cognito-> UserPool-> YourPool-> Apps-> Add App上取消选中'Generate client secret'
实际上,您需要指定标识池ID,而不是角色ARN(只要您在设置标识池时配置了UnAuth和Auth角色 - 这是默认设置) . 请参阅http://docs.aws.amazon.com/cognito/devguide/identity/getting-credentials/上的示例代码
如果你继续遇到麻烦,请告诉我们 .