使用 okhttp 向 AWS 发出 HTTP 请求

HTTP request to AWS with okhttp

我是 AWS 的新手,我想在我的 Android 应用程序中使用一项服务。不幸的是,这项服务 AWS AppConfig 还没有移动 SDK,所以我一直在尝试使用 okhttp 向 GetConfiguration API 发送 GET 请求。

签署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

当我向 GetConfiguration 发送请求时,它失败并显示

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.

我尝试向 GetDeploymentStrategy 发送请求并成功,所以我不认为这是我的凭据或我如何将它们附加到请求。

我认为问题在于我附加请求参数的方式,因为不需要额外请求参数的 API 成功(GetDeploymentStrategy 和 GetApplication),而需要 client_id 的 GetConfiguration 失败.

我的问题是:有没有例子说明如何处理签名者和请求的请求参数?

非常感谢。

花了更多时间尝试不同的东西,我找到了一些有用的东西。需要使用 com.amazonaws.Request 的 setParameters method. I did the same for the resource path and setResourcePath.

附加查询参数

创建待签名请求的示例:

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;
}