![](/img/trans.png)
[英]AWS Cognito - Developer Authenticated Identities in JavaScript(Browser)
[英]AWS Cognito Developer Authenticated Identities using JavaScript SDK
我需要使用JavaScript SDK實現Developer Authenticated Identities,但我遇到了問題。 我已經使用自定義身份驗證提供程序配置了標識池
在服務器上:
AWS.config = new AWS.Config({
region: 'ap-northeast-2',
credentials: new AWS.Credentials('XXXXXS7FJBAOO5IXXXXX', 'XXXXXYBo4jmfsu7K0qJSFvu3nlVvYOcVz4GXXXXX')
});
var params = {
IdentityPoolId: 'ap-northeast-2:a383cb2e-e302-4ff6-8d8f-70e3185XXXXX',
Logins: {
'com.abc.xyz': '9876543210' // different value for each user
}
};
var cognitoidentity = new AWS.CognitoIdentity();
cognitoidentity.getOpenIdTokenForDeveloperIdentity(params, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
}
else {
console.log(data); // successful response
}
});
服務器結果:
IdentityId: "ap-northeast-2:5cf7f3cd-b370-416b-bed8-f7f8c7aXXXXX"
Token: "eyJra.....sL8bg"
在瀏覽器上:
AWS.config = new AWS.Config({
region: 'ap-northeast-2'
});
var params = {
IdentityId: 'ap-northeast-2:5cf7f3cd-b370-416b-bed8-f7f8c7aXXXXX', //Received from server
CustomRoleArn: 'arn:aws:iam::356127965XXX:role/XXXXX_Customer',
Logins: {
'com.abc.xyz': '9876543210'
}
};
var cognitoidentity = new AWS.CognitoIdentity();
cognitoidentity.getCredentialsForIdentity(params, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
}
else {
console.log(data); // successful response
}
});
瀏覽器結果:
Please provide a valid public provider
基於這篇文章 ,我在瀏覽器部分進行了以下更改
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityId: 'ap-northeast-2:5cf7f3cd-b370-416b-bed8-f7f8c7aXXXXX', //Received from server
IdentityPoolId: 'ap-northeast-2:a383cb2e-e302-4ff6-8d8f-70e3185XXXXX',
Logins: {
'cognito-identity.amazonaws.com': '9876543210'
}
});
AWS.config.credentials.get(function(err, data) {
if (err) {
console.log(err); // an error occurred
}
else {
console.log(data); // successful response
}
});
AWS.config.credentials
現在我能夠收到包含accessKeyId,expireTime,secretAccessKey和sessionToken的響應
我意識到這是一個老帖子,但是如果有人遇到這個問題,我相信如果你改變了,你的第一種方法會有效:
Logins: {
'com.abc.xyz': '9876543210'
}
至
Logins: {
'cognito-identity.amazonaws.com': "eyJra.....sL8bg"
}
我覺得沒有使用您在步驟1中生成的令牌的任何解決方案都是不完整的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.