AWSCognito 配置错误中缺少区域

AWSCognito Missing region in config error

我在后端使用 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
});

在文档中说:

By default, credentials and region settings are left unconfigured. This should be configured by the application before using any AWS service APIs.

所以我设置了我的区域,但为什么我仍然收到错误消息?

您正在本地 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'
});

希望对您有所帮助!