AWS Cognito + RubyOnRails 在后端获取用户信息。 Aws::CognitoIdentityProvider::Errors::NotAuthorizedException

AWS Cognito + RubyOnRails get user info on the backend. Aws::CognitoIdentityProvider::Errors::NotAuthorizedException

我应该在 rails 上整合 cognito + ruby。

我的用户使用 cognito 默认登录页面登录并使用 url 参数重定向到我的页面。

https://development-my-site.auth.us-west-2.amazoncognito.com/oauth2/authorize?client_id=62i222222222222&redirect_uri=https://c3ffec02.ngrok.io/auth/cognito/callback&response_type=token&scope=email+openid+profile

重定向后我有参数

id_token=eyJraWQiOiIyYThzTzY3........
&access_token=eyJraWQiOiJDa0I2NGJsUFJKTWZrNGlV.....
&expires_in=3600
&token_type=Bearer

我应该从 url 获取 access_token 并传递给后端以进行用户验证。

在后端我使用AWS-SDK ```

  def client
    @client ||= Aws::CognitoIdentityProvider::Client.new(region: options.aws_region)
  end

  def get_user_info(params)
    client.get_user(access_token: params['access_token'])
  end

```

但是结果我有错误Aws::CognitoIdentityProvider::Errors::NotAuthorizedException (Access Token does not have required scopes):

如何获取用户信息?

您需要将范围 aws.cognito.signin.user.admin 添加到您的查询中:

    https://development-my-site.auth.us-west-2.amazoncognito.com/oauth2/authorize?
client_id=62i222222222222
    &redirect_uri=https://c3ffec02.ngrok.io/auth/cognito/callback
    &response_type=token
    &scope=email+openid+profile+aws.cognito.signin.user.admin

并在 A​​llowed OAuth Scopes

下的 Cognito 控制台中允许它