Android AWS APIGateway 身份验证 HttpURLConnection

Android AWS APIGateway Authentication HttpURLConnection

使用 Cognito 和 API 网关访问我们的 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
            );

然后尝试调用属于该 POOL 的我们的服务器。我一直回来 "Forbidden".

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

更新:

好的,通过 AWS 生成的 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();

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

aws api gateway