我想用Cognito暫時獲得AWS認證來訪問一些資源。找了一下相關的資訊,發現雖然官方支持JavaScript,但可能因為主要的用例是移動設備,資訊并不完整,盡管這樣我還是覺得我找到了正確的開始程式,如下:
首先:創建Cognito身份池(包括創建身份驗證和非驗證角色)
然后:運行AWS.CognitoIdentityCredentials.get()
第一步失敗了,是因為Cognito AWS端點沒有回應CORS headers。這對于移動/桌面app不是問題,但對于瀏覽器是個障礙,怎么解決這個問題啊?
基礎生成檔案附在最后,那里我移除了AWS賬戶ID。要想使用附件中的樣本檔案,得在AWS控制臺使用默認設定創建新的身份池并且從檔案中復制/粘貼region, account id, identity pool id 和 rolearn。
我從瀏覽器控制臺獲得的資訊如下:
XMLHttpRequest cannot load https://cognito-identity.eu-west-1.amazonaws.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://inncognito.s3-website-eu-west-1.amazonaws.com' is therefore not allowed access. The response had HTTP status code 404.
................
附上我的代碼:
Incognito error: Error: Network Failure {message: "Network Failure", code\: "NetworkingError", time: Thu Nov 27 2014 09:55:26 GMT+0100 (Central European Standard Time), region: "eu-west-1", hostname: "cognito-identity.eu-west-1.amazonaws.com"…}
最后附上基礎生成檔案:
<doctype html>
<html>
<head>
<title>AWS Cognito Unauthorized access in the Browser</title>
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.0.29.min.js"></script>
</head>
<body>
<script type="text/javascript">
AWS.config.region = 'eu-west-1';
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
AccountId: '123456789012',
IdentityPoolId: 'eu-west-1:b5dbefe5-c991-4fd1-b17b-aef2e1e5964f',
RoleArn: 'arn:aws:iam::123456789012:role/Cognito_rudimentaryUnauth_DefaultRole'
});
AWS.config.credentials.get(function(err) {
if (!err) {
console.log("Cognito Identity Id: " + AWS.config.credentials.identityId);
} else {
console.log('Incognito error: ', err);
}
});
</script>
</body>
</html>
有誰遇到類似的問題能幫我解答一下?謝謝!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/100219.html
標籤:AWS
