【问题标题】:AWS Cognito authorizer not working with AWS SDK for AndroidAWS Cognito 授权方不使用适用于 Android 的 AWS 开发工具包
【发布时间】:2020-01-01 21:25:47
【问题描述】:

在我的 Android 项目中,我使用 AWS SDK 注册用户,这要归功于 Cognito 并在 API Gateway 中调用 API。在我的 Cognito 用户池中,我创建了一个用户池组。该组的目的是只允许该组中的用户调用特定的 API。

为了让它发挥作用,我尝试关注this tutorial(尤其是视频)。因此,我创建了一个 Cognito 授权方,将其添加到 API Gateway 中的方法请求中,并尝试使用 AWS 开发工具包从我的应用程序调用 API:

@Service(endpoint = "https://abcdefghig.execute-api.eu-central-1.amazonaws.com/staging")
public interface AwsdemoapiClient
{
    @Operation(path = "/test-api", method = "GET")
    Empty testApiGet(); 
}

问题是:无论用户是否通过身份验证,是否在组中,当我在我的应用程序中调用testApiGet() 时,我总是收到以下错误:401 Unauthorized,即使我有正确的授权在我的 IAM 角色中。经过一番研究,似乎缺少 id 令牌,这可能是我收到该错误的原因。

但它不应该由适用于 Android 的 AWS 开发工具包自动管理吗?我该如何解决?

感谢您的帮助。

【问题讨论】:

    标签: amazon-web-services aws-api-gateway amazon-cognito aws-sdk-android


    【解决方案1】:

    在 header 中发送 id 令牌实际上解决了问题:

    @Service(endpoint = "https://abcdefghig.execute-api.eu-central-1.amazonaws.com/staging")
    public interface AwsdemoapiClient
    {
        @Operation(path = "/test-api", method = "GET")
        Empty testApiGet(@Parameter(name = "Authorization", location = "header") String idToken);; 
    }
    

    可以通过调用以下函数获取id token:

    CognitoUserPool pool = new CognitoUserPool(context, userPoolId, clientId, clientSecret, region);
    pool.getCurrentUser().getSessionInBackground(...);
    

    【讨论】:

      猜你喜欢
      • 2017-07-30
      • 1970-01-01
      • 1970-01-01
      • 2021-07-28
      • 2017-08-28
      • 1970-01-01
      • 2020-07-02
      • 2013-03-21
      • 2020-07-30
      相关资源
      最近更新 更多