Javascript Cognito:如何正确实现增强的简化身份验证流

Javascript Cognito:如何正确实现增强的简化身份验证流,javascript,amazon-web-services,amazon-cognito,Javascript,Amazon Web Services,Amazon Cognito,我正在尝试获取上描述的增强简化身份验证流 问题是我不知道如何正确使用SDK AWS.config.region = "ap-northeast-2" const cognitoParams = { IdentityPoolId: "ap-northeast-2:...", Logins: { "accounts.google.com": googleUser.getAuthResponse().id_token } } AWS.config.

我正在尝试获取上描述的增强简化身份验证流

问题是我不知道如何正确使用SDK

  AWS.config.region = "ap-northeast-2"
  const cognitoParams = {
    IdentityPoolId: "ap-northeast-2:...",
    Logins: {
      "accounts.google.com": googleUser.getAuthResponse().id_token
    }
  }
  AWS.config.credentials = new AWS.CognitoIdentityCredentials(cognitoParams)

  const identity = new AWS.CognitoIdentity()
  identity.getId(cognitoParams, function (err, identityId) {
    console.log(identityId)

    const identityParams = Object.assign({}, cognitoParams, {
      IdentityId: identityId
    })

    identity.getCredentialsForIdentity(identityParams, function (err, data) {
      console.log(data)
    })
  })
2
console.log
给出
null

AWS.config.region = "ap-northeast-2"
const cognitoParams = {
  IdentityPoolId: "ap-northeast-2:31cc246c-bd2e-46ee-91da-2b8eefcf0745",
  Logins: {
    "accounts.google.com": googleUser.getAuthResponse().id_token
  }
}
AWS.config.credentials = new AWS.CognitoIdentityCredentials(cognitoParams)

AWS.config.credentials.getId(function (err, identityId) {
  console.log(identityId)

  const identityParams = Object.assign({}, cognitoParams, {
    IdentityId: identityId
  })

  AWS.config.credentials.getCredentialsForIdentity(identityParams, function (err, data) {
    console.log(data)
  })
})
上面给出了标识,但由于无法读取未定义的的属性“getCredentialsForIdentity”而失败


我如何实现这一点

我发现下面的方法很有效。。。我应该从
CognitoIdentity
的实例调用函数,而不是
CognitoIdentityCredentials
。。。但这在文件中并不清楚

事实上,它使用了
CognitoIdentityCredentials
,原因是什么?我什么时候用这两种方法

  AWS.config.region = "ap-northeast-2"
  const cognitoParams = {
    IdentityPoolId: "ap-northeast-2:31cc246c-bd2e-46ee-91da-2b8eefcf0745",
    Logins: {
      "accounts.google.com": googleUser.getAuthResponse().id_token
    }
  }
  // AWS.config.credentials = new AWS.CognitoIdentityCredentials(cognitoParams)

  const identity = new AWS.CognitoIdentity()
  identity.getId(cognitoParams, function (err, identityData) {
    if (err) {
      return console.error(err)
    }

    const identityParams = {
      IdentityId: identityData.IdentityId,
      Logins: cognitoParams.Logins
    }

    identity.getCredentialsForIdentity(identityParams, function (err, data) {
      if (err) {
        return console.error(err)
      }
      console.log(data)
    })

我发现下面的工作。。。我应该从
CognitoIdentity
的实例调用函数,而不是
CognitoIdentityCredentials
。。。但这在文件中并不清楚

事实上,它使用了
CognitoIdentityCredentials
,原因是什么?我什么时候用这两种方法

  AWS.config.region = "ap-northeast-2"
  const cognitoParams = {
    IdentityPoolId: "ap-northeast-2:31cc246c-bd2e-46ee-91da-2b8eefcf0745",
    Logins: {
      "accounts.google.com": googleUser.getAuthResponse().id_token
    }
  }
  // AWS.config.credentials = new AWS.CognitoIdentityCredentials(cognitoParams)

  const identity = new AWS.CognitoIdentity()
  identity.getId(cognitoParams, function (err, identityData) {
    if (err) {
      return console.error(err)
    }

    const identityParams = {
      IdentityId: identityData.IdentityId,
      Logins: cognitoParams.Logins
    }

    identity.getCredentialsForIdentity(identityParams, function (err, data) {
      if (err) {
        return console.error(err)
      }
      console.log(data)
    })