【问题标题】:C# Amazon SP API CreateFeedDocument not workingC# Amazon SP API CreateFeedDocument 不起作用
【发布时间】:2021-08-12 17:05:46
【问题描述】:

通过 Amazon SP API 上传 Feed 有 3 个步骤,第一个步骤是通过 createFeedDocument API 获取 Feed 加密信息。但我收到Bad Request 的回复,内容如下:

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

C# 代码

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

我怀疑我没有正确使用restRequest.AddParameter,但我不确定。

我一直在关注以下链接:
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

更新 我也尝试替换以下行
restRequest.AddParameter("contentType", "application/xml; charset=UTF-8", ParameterType.RequestBody);

restRequest.AddJsonBody(new { contentType = "text/xml; charset=UTF-8" });

但后来我收到InvalidSignature 错误如下:

{
  "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"
    }
  ]
}

注意:我已经删除了一些标记的尾随字符并放置了......{我把它隐藏了}............

【问题讨论】:

  • 您发布的 GitHub 链接表明 their API consumes JSON,但您正在尝试使用它看起来像的 XML。会不会是这个问题?
  • 不,那是请求正文,我在对象下面发布为 json { "contentType": "application/xml; charset=UTF-8" }
  • 我看到你添加为查询参数marketpleaceid。您在文档中哪里看到的,因为他们的文档显示的只是一个正文参数。我收到了访问受限消息,现在想知道这是否是我签署请求的问题。
  • 你的问题解决了吗?我也面临同样的问题

标签: c# amazon-web-services rest amazonsellercentral selling-partner-api


【解决方案1】:

试试这个

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\"}");

【讨论】:

  • 虽然这不是必需的,但显然,我会在此处添加 why 这将解决 OP 问题以及他的方法可能存在的问题。这将使其他愿意阅读的人更好地理解您的解决方案:)
【解决方案2】:

我在我的开源库中这样做,它的工作没有问题

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

此处是创建和提交 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;

【讨论】:

  • 链接已失效。 404...
【解决方案3】:
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
}

IllegalLocationConstraintException未指定的位置约束与此请求发送到的区域特定端点不兼容。5B8EGG143CSG45XCi7DqESeN8Q/1QfotMvQIiijcEj/76sr2T+gNh9Ubhq5aQZ7SbZMXCIS/8Mgw6iDod2mCwX/LX6Q=

【讨论】:

    猜你喜欢
    • 2021-06-30
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-21
    • 2022-07-21
    • 2022-11-02
    相关资源
    最近更新 更多