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

0 回答 0