C# Amazon SP API CreateFeedDocument not working

2.1k Views Asked by At

There are 3 steps to upload a feed via Amazon SP API, the first one is to get feed encryption info via createFeedDocument API. But I am getting Bad Request as response with following content:

{
  "errors": [
    {
      "code": "InvalidInput",
      "message": "Invalid Input",
      "details": ""
    }
  ]
}

C# code

        private async Task<IRestResponse> CreateFeedDocument()
        {
            IRestRequest restRequest = new RestRequest("feeds/2020-09-04/documents", Method.POST);
            restRequest.AddParameter("contentType", "application/xml; charset=UTF-8", ParameterType.RequestBody);
            restRequest.AddQueryParameter("MarketplaceIds", "A21TJRUUN4KGV");
            restClient = new RestClient(live_url_base);
            restRequest = await signRequest(restRequest, restClient);
            return restClient.Execute(restRequest);
        }

        private async Task<IRestRequest> signRequest(IRestRequest restRequest, RestClient restClient)
        {
            var roleAcccess = await GetAssumeRoleTokenDetail();
            restRequest.AddHeader("x-amz-access-token", accessToken);

            AWSAuthenticationCredentials AWSCredentials = new AWSAuthenticationCredentials();
            AWSCredentials.AccessKeyId = roleAcccess.Credentials.AccessKeyId;
            AWSCredentials.SecretKey = roleAcccess.Credentials.SecretAccessKey;
            AWSCredentials.Region = region;

            restRequest.AddHeader("X-Amz-Security-Token", roleAcccess.Credentials.SessionToken);
            return new AWSSigV4Signer(AWSCredentials).Sign(restRequest, restClient.BaseUrl.Host);
        }

I doubt that I am not using restRequest.AddParameter properly, but I am not sure.

I have been following below links:
https://github.com/amzn/selling-partner-api-docs/blob/main/references/feeds-api/feeds_2020-09-04.md#createfeeddocument

https://github.com/amzn/selling-partner-api-docs/blob/main/guides/en-US/use-case-guides/feeds-api-use-case-guide/feeds-api-use-case-guide-2020-09-04.md#step-1-create-a-feed-document

How to Encrypt and upload data using selling-partner-api in Amzon using .net

Update I also tried replacing below line
restRequest.AddParameter("contentType", "application/xml; charset=UTF-8", ParameterType.RequestBody);
with
restRequest.AddJsonBody(new { contentType = "text/xml; charset=UTF-8" });

but then I was getting InvalidSignature error as below:

{
  "errors": [
    {
      "message": "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.

The Canonical String for this request should have been
'POST
/feeds/2020-09-04/documents

host:sellingpartnerapi-eu.amazon.com
x-amz-access-token:Atza|IwEBIA5KgrCsBbSXHmrXFS_FIgBTInh_xPAydLfi5q2P9xaFQf7p8Zl4NjqhHHxqRzUku__Q7GN1p2WQGRzuAoAa8oMkPLx57NJ05SqxEVXXG-fet3_XgKj8uBCU30HaGPsKltf4g2MD8Pqqt2OUrOXtkv4dAAjjCIxC-bFwVqOhvHktAur--NBv-bJaPZ608Av1GEu96GsNEV9eb0saVBwLaZD7NW3oOjzlCc8GPV9hdqHV5TUXY77QZgBLD1y94Vs1fSo54ShpyoMMOZebzbSr1K5gtf3wJZ.........................{ I hid it }...........................................
x-amz-date:20210524T175148Z
x-amz-security-token:FwoGZXIvYXdzEGsaDNUytY0xuP5/u61APiK2ARMZgv4IT+y2HLzdg5FjZOv6aL2bJ3baJPxBtCY2/7ASntTXfAF5s39P3/qspLLQfmqHPZiMGjweCE3Yf3aW5Q1mt+FLT2s2VUwuOawOQwDll51T2HB3wqyaDOSEpsWeR2Iym4TJXE2hbo7q5CQQBXissOo1Oruk5gcAp7uQHpnyuhCRCkfv/ErEpzdDA0JqfhMxdzmViVgsL1Kzalnbcy9lp+ACI4TL70iOl6j6xkyhFexe/aLXKLLPr4UGMi3Ver2CL6Q4kz.........................{ I hid it }...........................................

host;x-amz-access-token;x-amz-date;x-amz-security-token
4d719849acd655844ab5129f5e54a0ed16954c9580c7a9a737504faf42b309e2'

The String-to-Sign should have been
'AWS4-HMAC-SHA256
20210524T175148Z
20210524/eu-west-1/execute-api/aws4_request
a20e7ffe252dbf98d6a4b9213511ac1918f8bbad75ccbfd7ec46f5c9c1457b08'
",
     "code": "InvalidSignature"
    }
  ]
}

Note: I have removed some trailing chars of tokens and placed ......{ I hid it }..........

3

There are 3 best solutions below

1
On

try this one

restRequest.AddJsonBody("{\"contentType\":\"text/tab-separated-values; charset=UTF-8\"}");

RestClient restClient = new RestClient("https://sandbox.sellingpartnerapi-eu.amazon.com/");

IRestRequest restRequest = new RestRequest("/feeds/2021-06-30/documents", Method.POST);

restRequest.AddJsonBody("{\"contentType\":\"text/tab-separated-values; charset=UTF-8\"}");
1
On

I do that in my open source library and its work without problem

https://github.com/abuzuhri/Amazon-SP-API-CSharp

Here sample to create and submit feed

 ConstructFeedService createDocument = new ConstructFeedService("A3J37AJU4O9RHK", "1.02");

        var list = new List<PriceMessage>();
        list.Add(new PriceMessage()
        {
            SKU = "8201031206122...",
            StandardPrice = new StandardPrice()
            {
                currency = BaseCurrencyCode.AED.ToString(),
                Value = (201.0522M).ToString("0.00")
            }
        });
        createDocument.AddPriceMessage(list);

        var xml = createDocument.GetXML();

        var feedID = amazonConnection.Feed.SubmitFeed(xml, FeedType.POST_PRODUCT_PRICING_DATA);

        Thread.Sleep(1000*30);

        var feedOutput=amazonConnection.Feed.GetFeed(feedID);

        var outPut=amazonConnection.Feed.GetFeedDocument(feedOutput.ResultFeedDocumentId);

        var reportOutpit = outPut.Url;
0
On
private async Task UploadFile(byte[] bytes, string url)
{
  var contentType = "text/plain; charset=utf-8"; // this should be the same as what was used in Step #1 (in the CreateFeedDocument API request)

  RestClient restClient = new RestClient(url);
  IRestRequest restRequest = new RestRequest(Method.PUT);
  restRequest.AddParameter(contentType, bytes, ParameterType.RequestBody);

  var response = await restClient.ExecuteAsync(restRequest);

  if (!response.IsSuccessful)
  {
    // your error logic
  }

  // success. Move to Step #3
}

IllegalLocationConstraintExceptionThe unspecified location constraint is incompatible for the region specific endpoint this request was sent to.5B8EGG143CSG45XCi7DqESeN8Q/1QfotMvQIiijcEj/76sr2T+gNh9Ubhq5aQZ7SbZMXCIS/8Mgw6iDod2mCwX/LX6Q=