Android AWS APIGateway身份验证HttpURLConnection

时间:2016-06-02 16:39:45

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

使用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
            );

然后尝试调用我们的服务器,这是该POOL的一部分。我一直回来"禁止"。

我应该如何在HttpURLConnection的标题中使用credentialsProvider?

更新:

使用AWS Generated SDK正确使用API​​GateWay,但不确定如何更改生成的文件:

    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();

1 个答案:

答案 0 :(得分:2)

如果您使用的是AWS API Gateway,则可以在不使用AWS Cognito的情况下单独使用它。 AWS API Gateway是一种http服务,就像服务器的代理或AWS Lambda Function的网关一样。

aws api gateway