首页 > 解决方案 > CDON API RESTful Api GET 请求

问题描述

我目前正在从 cdon 获取客户数据,它是一个电子商务平台。他们在这里有他们的 API 文档:

CDON API 文档

首先让我向您展示我的代码:

myToken = '<token here>'
myUrl = 'https://admin.marketplace.cdon.com/api/reports/d8578ef8-723d-46cb-bb08-af8c9b5cca4c'
head = {'Authorization': 'token {}'.format(myToken),
'Status':'Online',
'format':'json'}
filters = '?filter={"Status":["Online"],"format": ["json"] }}'
response = requests.get(myUrl + filters, headers=head)
report = response.json()
print(report.products)

这仅返回参数。例如在这个 JSON:CDON Github

状态有一个值Onlineonline是一组我只想获取的项目。

我想要得到的是这样的回应:

{

  "Products": [

    {

      "SKU": "322352",

      "Title": "Fabric Cover",

      "GTIN": "532523626",

      "ManufacturerArticleNumber": "",

      "StatusCDON": "Online",

      "ExposeStatusCDON": "Buyable",

      "InStock": 0,

      "InStockCDON": 0,

      "CurrentPriceSE": null,

      "OrdinaryPriceSE": null,

      "CurrentPriceCDONSE": 299.0000,

      "OrdinaryPriceCDONSE": null,

      "CurrentPriceDK": null,

      "OrdinaryPriceDK": null,

      "CurrentPriceCDONDK": null,

      "OrdinaryPriceCDONDK": null,

      "CurrentPriceNO": null,

      "OrdinaryPriceNO": null,

      "CurrentPriceCDONNO": null,

      "OrdinaryPriceCDONNO": null,

      "CurrentPriceFI": null,

      "OrdinaryPriceFI": null,

      "CurrentPriceCDONFI": null,

      "OrdinaryPriceCDONFI": null

    },

这意味着在线项目的完整列表

我应该怎么放这个......在我尝试过的所有 API 中,这个非常令人困惑,这甚至是 RestFul 吗?如果我可以实现此 C# 示例代码的 python 等效项:

public string Post(Guid repordId, string path)
{
  var filter = new JavaScriptSerializer().Serialize(new
  {
         States = new[] { "0" } // Pending state
  });

  var content = new FormUrlEncodedContent(new[]
  {
         new KeyValuePair("ReportId", repordId.ToString()),
         new KeyValuePair("format", "json"),
         new KeyValuePair("filter", filter)
  });

  var httpClient = new HttpClient() { BaseAddress = new Uri("https://admin.marketplace.cdon.com/") };
  httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("api", ApiKey);
  var response = httpClient.PostAsync(path, content).Result;
  response.EnsureSuccessStatusCode();

  return response.Content.ReadAsStringAsync().Result;
}

我可能无法理解这个 API 是如何工作的,我得到的响应是从他们的 JSON 格式的报告函数中手动获取的。

图片

我做了很多尝试,在那个代码(我的代码)我停止了,在这上面呆了 4 个小时让我放弃并问。相信我已经搜索了尽可能多的参考资料。这真的很混乱。

如何获得我想要的响应?通过网址过滤?或通过标题?这还算安宁吗?帮助T_T

标签: python-3.xapirestful-authentication

解决方案


文档在第一行中指出,强调我的:

为了生成报告,您使用您希望用于报告的参数对报告 API执行 POST 调用。

您的 Python 代码没有发出 POST 请求,您正在尝试 GET 请求。文档继续

[...] 要过滤瑞典订单,您将CountryCodes 属性设置为“瑞典”,并且要返回和取消订单,您将 States 属性设置为 2 和 3。所以最终过滤器将如下所示:

{
    "CountryCodes": [ "Sweden" ],
    "States": ["2", "3"]
}

因此,您需要准备一个带有所需过滤器的filter对象(Python 中的字典)。幸运的是,字典的 Python 语法是等价的(Python 很灵活,也允许使用单引号字符串):

filter = {
    'CountryCodes': [ 'Sweden' ],
    'States': [ '0' ]
}

文档继续

然后,您将参数作为表单数据 ( content-type: application/x-www-form-urlencoded) 发布,因此请求正文将如下所示:

ReportId=d4ea173d-bfbc-48f5-b121-60f1a5d35a34&format=json&filter={"CountryCodes":["Sweden"],"States":["2","3"]}

application/x-www-form-urlencoded是 HTTP 发布的默认设置,请求模块知道这一点并自动为您执行此操作。您需要做的就是准备一个data包含您要发布的数据的字典。

data = {
    'ReportId': 'd4ea173d-bfbc-48f5-b121-60f1a5d35a34',
    'format': 'json'
    'filter': json.dumps(filter)
}

filter参数应该是 JSON 格式。您必须自己通过json.dumps().

import json

head = { ... as above }
filter = { ... as above }
data = { ... as above }

response = requests.post(url, data, header=head)

我将把正确设置 Authorization 标头作为练习留给你。部分是因为它并不难,部分是因为我无意在这个网站上创建一个 API 密钥来测试它,部分是因为您当前的标头完全有可能已经工作。


推荐阅读