问题描述
我想用Cognito暂时获得AWS认证来访问一些资源。找了一下相关的信息,发现虽然官方支持JavaScript,但可能因为主要的用例是移动设备,信息并不完整,尽管这样我还是觉得我找到了正确的开始程序,如下:首先:创建Cognito身份池(包括创建身份验证和非验证角色)然后:运行AWS.CognitoIdentityCredentials.get()第一步失败了,是因为CognitoAWS端点没有响应CORSheaders。这对于移动/桌面app不是问题,但对于浏览器是个障碍,怎么解决这个问题啊?基础生成文件附在最后,那里我移除了AWS账户ID。要想使用附件中的样本文件,得在AWS控制台使用默认设置创建新的身份池并且从文件中复制/粘贴region,accountid,identitypoolid和rolearn。我从浏览器控制台获得的信息如下:XMLHttpRequestcannotloadhttps://cognito-identity.eu-west-1.amazonaws.com/.No'Access-Control-Allow-Origin'headerispresentontherequestedresource.Origin'http://inncognito.s3-website-eu-west-1.amazonaws.com'isthereforenotallowedaccess.TheresponsehadHTTPstatuscode404.................附上我的代码:Incognitoerror:Error:NetworkFailure{message:"NetworkFailure",code:"NetworkingError",time:ThuNov27201409:55:26GMT+0100(CentralEuropeanStandardTime),region:"eu-west-1",hostname:"cognito-identity.eu-west-1.amazonaws.com"…}最后附上基础生成文件:<doctypehtml><html><head><title>AWSCognitoUnauthorizedaccessintheBrowser</title><scriptsrc="https://sdk.amazonaws.com/js/aws-sdk-2.0.29.min.js"></script></head><body><scripttype="text/javascript">AWS.config.region='eu-west-1';AWS.config.credentials=newAWS.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("CognitoIdentityId:"+AWS.config.credentials.identityId);}else{console.log('Incognitoerror:',err);}});</script></body></html>有谁遇到类似的问题能帮我解答一下?谢谢!