[带有okhttp的对AWS的HTTP请求

问题描述 投票:0回答:1

我对AWS来说还很陌生,我想在我的Android应用中使用一项服务。不幸的是,该服务AWS AppConfig还没有移动SDK,因此我一直在尝试使用okhttp向GetConfiguration API发送GET请求。

要签署request,我正在使用AWS Android SDK中的AWS4Signer。我正在为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失败。

我的问题是:有没有使用签名者和请求处理请求参数的示例?

非常感谢。

android amazon-web-services okhttp aws-amplify aws-amplify-sdk-android
1个回答
0
投票

[花了更多时间尝试不同的东西,而我有一些可行的方法。需要使用com.amazonaws.Request的setParameters方法附加查询参数。我对资源路径和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;
}
© www.soinside.com 2019 - 2024. All rights reserved.