如何使用 Restsharp 从 Amazon SP-API 请求受限数据令牌 (RDT)

发布于 2025-01-10 01:59:36 字数 2169 浏览 3 评论 0原文

为了连接到 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);
    }
}

To connect to amz sp-api, amazon uses Restsharp in their samples.
I have usually worked with httpwebrequests and alikes.
Following Amazon's documentation, I have been able to navigate it and create a test-environment.
Now I have hit a wall trying to request a RDP token request.

Test Scenario: I am trying to get the details of existing, unfiltered Orders. These contains customer private data. Thus, the RDP requirement.

I understand what I need to provide in my request, but I am failing in HOW to pass it through the RestRequest.
Amazon samples on this part are available on Java only, and I haven't seen any guidance on how to substitude the Java native libraries to a C# environment.
All information sites I have investigated just relink to the C# model samples or to the raw samples in their documentation.

Could someone give me an example -or point me to documentation where can I learn these bases- on how to add this raw to the request using 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"
}

In case it could be useful: Here is my absolute garbage test -after too many hours and redoes of trials and error.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

物价感观 2025-01-17 01:59:36

编辑

几个月前他们已经解决了这个问题,现在它按照他们指定的方式运行良好在他们的文档中

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

OLD

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

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

EDIT

Few months ago they already solved this issue, now it's working fine the way they specify 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"]
    }
  ]
}

OLD

restrictedResources isn't an array, it is a object.
Call it with a body like this one:

{"restrictedResources": 
{
  "method": "GET",
  "path": "/orders/v0/orders",
  "dataElements": ["buyerInfo", "shippingAddress", "buyerTaxInformation"]
}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文