简体   繁体   中英

HTTP request to AWS with okhttp

I'm pretty new to AWS and I'd like to use a service in my Android app. Unfortunately, this service, AWS AppConfig, doesn't have a mobile SDK yet, so I've been trying to send a GET request to the GetConfiguration API with okhttp.

To sign the request , I'm using AWS4Signer from the AWS Android SDK. I'm providing credentials with an implementation of AWSCredentials .

    com.amazonaws.Request requestAws = new DefaultRequest(amazonWebServiceRequest, serviceName);
    URI uri = URI.create("https://appconfig.us-west-2.amazonaws.com/applications/[applicationID]/environments/[environmentID]/configurations/[configurationID]?client_id=ClientId");
    requestAws.setEndpoint(uri);
    requestAws.setHttpMethod(HttpMethodName.GET);
    AWS4Signer signer = new AWS4Signer();
    signer.setServiceName(serviceName);
    signer.setRegionName(Regions.US_WEST_2.getName());
    signer.sign(requestAws, credentials);
    // get relevant authorization headers from requestAws and insert them in the okhttp request as headers

When I send a request to GetConfiguration, it fails with

The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.

I tried sending a request to GetDeploymentStrategy , and that was successful, so I don't think it's my credentials or how I'm attaching them to the request.

I think the problem is with how I'm attaching the request parameters, since the APIs that don't require additional request params succeed (GetDeploymentStrategy and GetApplication), while GetConfiguration, which requires client_id , fails.

My question is: are there any examples of how to handle request parameters with the signer and request?

Thanks very much.

Spent some more time trying different things and I have something that works. Query parameters need to be attached using com.amazonaws.Request's setParameters method. I did the same for the resource path and setResourcePath .

Example for creating the request to be signed:

private static String endpoint = "https://appconfig.us-west-2.amazonaws.com";
private static String resourcePath = "/applications/[applicationID]/environments/[environmentID]/configurations/[configurationID]";
private static String parameters = "?client_id=hello&client_configuration_version=0";
private static String fullUrl = endpoint + resourcePath + parameters;
private static String serviceName = "appconfig";

private com.amazonaws.Request createAwsRequest() {
    AmazonWebServiceRequest amazonWebServiceRequest = new AmazonWebServiceRequest() {
    };

    com.amazonaws.Request requestAws = new DefaultRequest(amazonWebServiceRequest, serviceName);

    URI uri = URI.create(endpoint);
    requestAws.setEndpoint(uri);
    requestAws.setResourcePath(resourcePath);
    Map<String, String> params = new HashMap<>();
    params.put("client_id", "hello");
    params.put("client_configuration_version", "0");
    requestAws.setParameters(params);
    requestAws.setHttpMethod(HttpMethodName.GET);

    return requestAws;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM