AWS Cognito配置中缺失区域错误

4
我在后端使用aws-sdk javascript,可以成功使用AWS,但是当我尝试使用getOpenIdTokenForDeveloperIdentity方法时,响应返回"Missing region in config error"
var config = new AWS.Config({
  accessKeyId: "MYACCESSKEY", secretAccessKey: "MYSECRETYKEY", region: 'us-east-1'
});

var params = {
  IdentityPoolId: 'MYIDENTITYPOOLID', /*   required */
  Logins: { /* required */
    "login.my.myapp": 'string',
    /* anotherKey: ... */
  },
  IdentityId: null,
  TokenDuration: 0
};

cognitoidentity.getOpenIdTokenForDeveloperIdentity(params,function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else console.log(data);               // successful response
});

在文档中提到:
默认情况下,凭证和区域设置未配置。在使用任何 AWS 服务 API 之前,应用程序应该对其进行配置。
因此,我设置了我的区域,但为什么仍然出现错误?

1个回答

9
你正在将区域设置在本地的config变量中,而应该在全局AWS.config中进行设置,如下所示:
AWS.config.region = 'us-east-1';

同样适用于凭据。如果您想为所有AWS客户端使用Amazon Cognito凭证,则应像这样初始化AWS.config.credentials:

AWS.config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityPoolId: 'YOUR_POOL_ID'
});

我希望这有所帮助!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接