AWSCognito配置错误中缺少区域

abc*_*bcf 3 amazon-web-services amazon-cognito aws-sdk

我正在使用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
});
Run Code Online (Sandbox Code Playgroud)

在文档中它说:

默认情况下,凭据和区域设置保持未配置状态.在使用任何AWS服务API之前,应由应用程序配置此项.

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

Alb*_*ora 6

您正在本地config变量中设置区域.它应该在全局设置AWS.config,如下所示:

AWS.config.region = 'us-east-1';
Run Code Online (Sandbox Code Playgroud)

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

AWS.config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityPoolId: 'YOUR_POOL_ID'
});
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助!