0

I'm trying to use Aws4RequestSigner in a VS2015 form to sign a search request to Amazon PAAPI.

https://www.nuget.org/packages/Aws4RequestSigner/

I get this response from the API:

{"__type":"com.amazon.paapi5#IncompleteSignatureException","Errors": [{"Code":"IncompleteSignature","Message":"The request signature did not include all of the required components. If you are using an AWS SDK, requests are signed for you automatically; otherwise, go to https://webservices.amazon.com/paapi5/documentation/sending-request.html#signing."}]}

private async void Form1_Load(object sender, EventArgs e)
        {
            _accessKey = "x";
            _secretKey = "x";
            _service = "ProductAdvertisingAPIv1";
            _region = "us-east-1";
            _requestUri = new Uri("https://webservices.amazon.com/paapi5/searchitems");
            var payload = new
            {
                Keywords = "Harry",
                Marketplace = "www.amazon.com",
                PartnerTag = "x0d-20",
                PartnerType = "Associates",
                Resources = new string[] { "Images.Primary.Small", "ItemInfo.Title", "Offers.Listings.Price" },
                SearchIndex = "All"
            };
            string jsonString = JsonConvert.SerializeObject(payload);
            var content = new StringContent(jsonString, Encoding.UTF8, "application/json");
            var xAmzDate = GetTimeStamp();
            content.Headers.Add("content-encoding", "amz-1.0");
            content.Headers.Add("x-amz-date", xAmzDate);
            content.Headers.Add("x-amz-target", "com.amazon.paapi5.v1.ProductAdvertisingAPIv1.SearchItems");
            var request = new HttpRequestMessage
            {
                Method = HttpMethod.Post,
                RequestUri = _requestUri,
                Content = content
            };
            request.Headers.Host = "webservices.amazon.com";
            var contentType = new MediaTypeHeaderValue("application/json");
            contentType.CharSet = "utf-8";
            request.Content.Headers.ContentType = contentType;
            var signer = new AWS4RequestSigner(_accessKey, _secretKey);
            request = await signer.Sign(request, _service, _region);
            try
            {
                var client = new HttpClient();
                var response = await client.SendAsync(request);

                if (!response.IsSuccessStatusCode)
                {
                    var error = await response.Content.ReadAsStringAsync();

                }
                // response.EnsureSuccessStatusCode();
                txtDisplay.Text = await response.Content.ReadAsStringAsync();

            }
            catch (HttpRequestException ex)
            {
                string error = ex.Message;
                txtDisplay.Text = error;
            }

        }

private string GetTimeStamp()
        {
            return DateTime.UtcNow.ToString("yyyyMMdd\\THHmmss\\Z");
        }

It could be that the headers are being added incorrectly or Aws4RequestSigner is simply outdated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0

I also tested Aws4RequestSigner and it does not work anymore - perhaps some changes in the API.

But I found another project https://github.com/AbdallahAbuhussein/AmazonAuthSignatureV4 that partially works and managed to get a working solution

Only the signing part works - the ApiCaller part does not. The AwsAuthSignaturCreator is in fact the C# version of the Java example from Amazon website (https://webservices.amazon.com/paapi5/documentation/without-sdk.html)

I managed to get product info using only the AwsAuthSignaturCreator part with a code like this (you need to replace productId,tag and keys)

 public void CallAmazon()
    {
        var productId = "XXXXXXXXXX";
        string productResources = "\"Images.Primary.Small\",\"Images.Primary.Medium\",\"ItemInfo.Title\"";
        var initHeaders = new Dictionary<string, string>()
        {
            {"content-encoding", "amz-1.0" },
            {"content-type", "application/json; charset=utf-8" },
            {"host", "webservices.amazon.com" },
            {"x-amz-target", "com.amazon.paapi5.v1.ProductAdvertisingAPIv1.GetItems" }
        };
        var payload = "{\"Marketplace\":\"www.amazon.com\",\"PartnerType\":\"Associates\",\"Operation\":\"GetItems\",\"PartnerTag\":\"" + <ASSOCIATETAG-20>+ "\",\"ItemIds\":[\""
            + productId + "\"],\"Resources\":[" + productResources + "]}";

        var creator = new AwsAuthSignaturCreator(ACCESS KEY, SECRET KEY, "/paapi5/getitems", "us-east-1", "ProductAdvertisingAPI", "POST", initHeaders, payload);
        var headers = creator.GetHeaders();
        var requestUri = "https://webservices.amazon.com/paapi5/getitems";

        WebClient client = new WebClient();
        foreach (KeyValuePair<string, string> entrySet in headers)
        {
            client.Headers.Add(entrySet.Key, entrySet.Value);
        }
        var result = client.UploadData(requestUri, Encoding.UTF8.GetBytes(payload));


        var json = Encoding.UTF8.GetString(result);
      
    }