简体   繁体   English

通过 API 从 Shopware 5 获取订单列表

[英]Get order list from Shopware 5 via API

I'm in the process of building a small (python) tool to retrieve orders from my SW5 store via API.我正在构建一个小型(python)工具以通过 API 从我的 SW5 商店检索订单。

For this, I combined the username and the API key into a string (separated by ":") and then converted the whole thing as a bytestring.为此,我将用户名和 API 密钥组合成一个字符串(以“:”分隔),然后将整个字符串转换为字节串。 This bytestring was then base64 "encoded" and specified as a header as follows:这个字节串然后被 base64“编码”并指定为标题,如下所示:

` `

def get_order_shopware5():
    
    header = {"Authorization": "Basic NjE2NDZkNjk2ZTNhNTM2ZTY1NzA0OTZlNmI2YzRhNjQ2YzY0NTA1MTM1Mzg0NjdhN2E0ODRlMzk3OTZiNGU2NDZlNzA2ODM1Nzk2YzU0NWEzODM2NjQ1MDZkNTM"}
    print(header)
    
    res = requests.get("https://shopname.de/api/orders", headers=header)
    
    print(res.content)

` But when I call the function, I always get a "b'{"success":false, "message": "Invalid or missing auth"}'" as a response. ` 但当我调用该函数时,我总是会收到“b'{"success":false, "message": "Invalid or missing auth"}'"作为响应。

When I manually access www.shopname.de/api/orders via the browser and enter the credentials, everything works fine.当我通过浏览器手动访问www.shopname.de/api/orders并输入凭据时,一切正常。 So I'm assuming that there's something hanging on the synthax or the credential conversion.所以我假设合成器或凭据转换上有问题。 But I can't figure out what exactly.但我无法弄清楚到底是什么。

I am grateful for any hints!我很感激任何提示!

Greetz, Lama问候,喇嘛

PS: I've tried multiple versions of the header synthax as well as different ways of converting the original string to a bytestring (with/without whitespaces, with/without using full bytes). PS:我已经尝试了多个版本的标头合成器以及将原始字符串转换为字节串的不同方法(有/没有空格,有/没有使用完整字节)。 But nothing worked so far.但到目前为止没有任何效果。

If anyone is interested:如果有人感兴趣:

The first thing is an error in the URL -> "https://shopname.de/api/orders/" instead of "https://shopname.de/api/orders".首先是 URL 错误 -> “https://shopname.de/api/orders/”而不是“https://shopname.de/api/orders”。 The devil is in the detail:D.细节决定成败:D。

The second thing is a slight confusion by the Shopware documentation.第二件事是 Shopware 文档造成的轻微混淆。 It says:它说:

  1. Combine user and key in a string - separated by ":".将用户和密钥组合在一个字符串中 - 用“:”分隔。
  2. Convert this to an octet (byte) string将其转换为八位字节(字节)字符串
  3. Convert the resulting string to a base64 string and prepend "Basic ".将生成的字符串转换为 base64 字符串并在前面添加“Basic”。
  4. Create a header like this -> Authorization: Basic [KEY_VALUE]像这样创建标题 -> Authorization: Basic [KEY_VALUE]

3/4 are correct. 3/4 是正确的。 If you skip step 2 everything works fine.如果您跳过第 2 步,一切正常。

Greetz格雷茨

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM