【问题标题】:Android AWS APIGateway Authentication HttpURLConnectionAndroid AWS APIGateway 身份验证 HttpURLConnection
【发布时间】:2016-10-02 12:38:28
【问题描述】:

在使用 Cognito 和 API Gateway 访问我们的 REST API 时遇到问题

我一辈子都找不到一个简单的例子来说明如何在我们的 AWS 服务器上调用安全的 REST api。

我打电话

CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                    ProfileViewActivity.this, // get the context for the current activity
                    AMAZON_COGNITO_IDENTITY_POOL_ID, // your identity pool id
                    Regions.US_EAST_1  //Region
            );

然后尝试调用我们的服务器,该服务器是该池的一部分。我不断地回到“禁止”。

我应该如何处理 HttpURLConnection 标头中的 credentialsProvider ?

更新:

可以通过 AWS Generated SDK 以正确的方式使用 APIGateWay,但不确定如何更改生成的文件:

    private class MyAsyncTask extends AsyncTask<String, Void, String> {
    private TextView textView;

    public MyAsyncTask() {
    }

    @Override
    protected String doInBackground(String... strings) {
        AWSCredentialsProvider credenetialsProvider = new CognitoCachingCredentialsProvider(
                ProfileViewActivity.this,          // activity context
                AMAZON_COGNITO_IDENTITY_POOL_ID, // Cognito identity pool id
                Regions.US_EAST_1 // region of Cognito identity pool
        );

        ApiClientFactory factory = new ApiClientFactory()
                .credentialsProvider(credenetialsProvider)
                .region("us-east-1")
                .endpoint("https://myendpoint")
                .apiKey("xxxxxxxxxxxxxxxxxxxxxxx");


        // MyClient is the AWS Android SDK Generated class

            final MyClient client = factory.build(MyClient.class);

         client.feedGet();

        String str  = client.testGet().toString();


        Log.d("###", "here after test" +client.testGet().toString());


        return "DONE";
    }

    @Override
    protected void onPostExecute(String temp) {
        Log.d("####", "onPostExecute");
    }
}

然后

    private class MyAsyncTask extends AsyncTask<String, Void, String> {
    private TextView textView;

    public MyAsyncTask() {
    }

    @Override
    protected String doInBackground(String... strings) {
        AWSCredentialsProvider credenetialsProvider = new CognitoCachingCredentialsProvider(
                ProfileViewActivity.this,          // activity context
                AMAZON_COGNITO_IDENTITY_POOL_ID, // Cognito identity pool id
                Regions.US_EAST_1 // region of Cognito identity pool
        );

        ApiClientFactory factory = new ApiClientFactory()
                .credentialsProvider(credenetialsProvider)
                .region("us-east-1")
                .endpoint("https://myendpoint")
                .apiKey("xxxxxxxxxxxxxxxxxxxxxxx");


        // MyClient is the AWS Android SDK Generated class

            final MyClient client = factory.build(MyClient.class);

         client.feedGet();

        String str  = client.testGet().toString();


        Log.d("###", "here after test" +client.testGet().toString());


        return "DONE";
    }

    @Override
    protected void onPostExecute(String temp) {
        Log.d("####", "onPostExecute");
    }
}

========= MyClient.java - AWS Android SDK 生成文件

    @com.amazonaws.mobileconnectors.apigateway.annotation.Service(endpoint = https:myaws_server)
public interface MyClient {
    @com.amazonaws.mobileconnectors.apigateway.annotation.Operation(path = \"/test\", method = "GET")
    Empty testGet();

【问题讨论】:

  • 您使用的是生成的Android API Gateway SDK吗?在这种情况下,您应该能够使用来自 Cognito 的凭证提供程序来实例化一个新的 API Gateway 客户端,并且这些请求应该可以工作。如果您在使用 API Gateway 客户端的地方共享代码,可能会有所帮助。
  • 我已经用我正在使用的代码更新了问题 - 更改为 API Gateway 但它没有因代理错误而失败
  • 你好。你有什么成功吗?我也面临同样的问题
  • 嘿@Krishnavrinsoft,我终于回到 APIGateway 并这样做了。一旦我能够看到传回的 JSON,我就能够更新 ASK 生成的代码并取回值。如果你想走这条路,请告诉我,我会提供一个教程。 AWS 人员告诉我他们将更新文档,以便我们拭目以待。
  • 感谢您的回复。是的,我需要教程。我还面临 2 周的问题。

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


【解决方案1】:

如果您使用的是 AWS API Gateway,您可以独立使用它而无需使用 AWS Cognito。 AWS API Gateway 是一种 http 服务,就像您的服务器的代理或 AWS Lambda 函数的网关一样。

aws api gateway

【讨论】:

  • 感谢@cenxui-lin,但是当我尝试得到“无法评估 $Proxy1.toString() 并且不知道这意味着什么时。
  • Cognito是不用API网关直接使用AWS服务的方式,我猜你用Cognito访问API网关是错误的,如果我戴错了,请纠正我。
  • 嘿@cenxui,我不认为你错了,我只是无法访问使用Cognito身份验证的url。
猜你喜欢
  • 2012-01-26
  • 1970-01-01
  • 2013-09-05
  • 1970-01-01
  • 1970-01-01
  • 2016-10-22
  • 2021-05-14
  • 1970-01-01
  • 2014-08-05
相关资源
最近更新 更多