【问题标题】:How to request Restricted Data Token (RDT) from Amazon SP-API using Restsharp如何使用 Restsharp 从 Amazon SP-API 请求受限数据令牌 (RDT)
【发布时间】:2022-10-20 19:44:26
【问题描述】:

为了连接到 amz sp-api,亚马逊在他们的示例中使用了 Restsharp。 我通常使用 httpwebrequests 等。 按照亚马逊的文档,我已经能够浏览它并创建一个测试环境。 现在我在尝试请求 RDP 令牌请求时碰壁了。

测试场景:我正在尝试获取现有的未过滤订单的详细信息。这些包含客户私人数据。因此,RDP 要求。

我了解我需要在我的请求中提供什么,但我无法通过 RestRequest 传递它。 这部分的 Amazon 示例仅在 Java 上可用,我还没有看到任何关于如何将 Java 本机库替换为 C# 环境的指南。 我调查过的所有信息站点都只是重新链接到 C# 模型示例或文档中的原始示例。

有人可以给我一个例子 - 或者指向我在哪里可以学习这些基础的文档 - 关于如何使用 Restsharp 将此原始添加到请求中?

POST https://sellingpartnerapi-na.amazon.com/tokens/2021-03-01/restrictedDataToken
{
  "restrictedResources": [
    {
      "method": "GET",
      "path": "/orders/v0/orders/123-1234567-1234567",
      "dataElements": ["buyerInfo", "shippingAddress"]
    }
  ],
  "targetApplication": "amzn1.sellerapps.app.target-application"
}

如果它可能有用:这是我的绝对垃圾测试 - 经过太多小时和重做试验和错误。

const string END_POINT = "https://sellingpartnerapi-eu.amazon.com";
const string APP_ID = "amzn1.sp.solution.*****";

public void RDT_Request()
{
    RestClient restClient = new RestClient(END_POINT);
    string request_url = END_POINT + "/tokens/" + DateTime.Now.ToString("yyyy-MM-dd") + "/restrictedDataToken";
    IRestRequest restRequest = new RestRequest(request_url, Method.POST);

    Console.Write("Generating request.");
    restRequest.AddHeader("content-type", "application/json");
    restRequest.AddHeader("user-agent", "amz sp-api demo (Language=csharp;Platform=Windows/10)");

    string jsonBody = "{\"restrictedResources\": " +
            "[{\"method\": \"GET\", " +
            "\"path\": \"/orders/v0/orders\", " +
            "\"dataElements\": [\"buyerInfo\", \"shippingAddress\"]}]," +
            "\"targetApplication\": \"" + APP_ID + "\"}";
    restRequest.AddJsonBody(jsonBody);

    try
    {
        Console.Write("Executing request.");
        var result = restClient.Execute(restRequest);
        if (result.StatusCode == HttpStatusCode.OK)
        {
            Console.WriteLine(" - Sucess:\n" + result.Content);
            return;
        }
        throw new Exception("ERROR " + result.StatusCode.ToString());
    }
    catch (Exception e)
    {
        Console.WriteLine(" - " + e.Message);
    }
}

【问题讨论】:

  • 您是否尝试过阅读 RestSharp 文档?
  • 我现在正在研究它-我知道我的示例代码中有一些明显的错误-。在连续几天关注亚马逊的文档后,我出于纯粹的绝望发布了这个问题。每一步都是一个不同的兔子洞。如果我在修复该代码后设法获得令牌,我将发布答案。
  • 我只能就 RS 107 提出建议(您的代码适用于 RS <=106)。一些提示: (1) 移除 content-type 头 (2) 移除 user-agent 头,使用客户端选项 UserAgent 属性。 (3) 要么为有效负载创建一个类型,要么将AddStringBody 与内容类型一起使用。 (4) 从请求 URL 中删除 END_POINT
  • @Siladamart 你能解决这个问题吗?

标签: c# restsharp amz-sp-api


【解决方案1】:

编辑

几个月前他们已经解决了这个问题,现在他们指定in their doc 的方式工作正常。

POST https://sellingpartnerapi-na.amazon.com/tokens/2021-03-01/restrictedDataToken
{
  "restrictedResources": [
    {
      "method": "GET",
      "path": "/orders/v0/orders",
      "dataElements": ["buyerInfo", "shippingAddress", "buyerTaxInformation"]
    }
  ]
}

老的

restrictedResources 不是一个数组,它是一个对象。 用这样的身体来称呼它:

{"restrictedResources": 
{
  "method": "GET",
  "path": "/orders/v0/orders",
  "dataElements": ["buyerInfo", "shippingAddress", "buyerTaxInformation"]
}}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-03
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 2017-11-12
    • 2015-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多