python-3.x - 如何使用 Python 和 WC-API 从 WooCommerce 软件插件中获取 api 信息
问题描述
我试图弄清楚如何在 Python 和 WooCommerce Api 的帮助下从 WooCommerce 软件插件获取许可证密钥。可用的端点似乎不起作用。这里有没有人知道如何获取东西结合以上...?
亲切的问候,
杜威
根据 WC-Api 的文档(http://woocommerce.github.io/woocommerce-rest-api-docs/#introduction)
print(wcapi.get("api-keys").json()) 应该返回信息,因为它是 WooCommerce 的软件插件添加的端点(https://docs.woocommerce.com/document/software-add-开/)。但这会返回失败的连接:{'code': 'rest_no_route', 'message': 'No route was found matching the URL and request method', 'data': {'status': 404}} 作为消息。
{'code': 'rest_no_route', 'message': '没有找到匹配 URL 和请求方法的路由', 'data': {'status': 404}}
解决方案
这是如何做到的:
在开始之前,如果您还没有安装下面的库,则需要安装。您可以像这样使用 pip:
pip install WooCommerce
pip install requests
编码:
from woocommerce import API
import requests
wcapi = API(
url="website_url",
consumer_key="consumerkey", # You can find it on your woocommerce dashboard
consumer_secret="consumersecret", # You can find it on your woocommerce dashboard
version="wc/v3"
)
licence_email = "email" # the email that is associated with the licence
licence = "licence" # the licecne itself
product = "product_name" # The name of the product which you sell the licence for
website_url = "url" # set the website url
request = wcapi.get(f"{website_url}/?wc-api=software-api&request=check&email={licence_email}&request=check&license_key={licence}&request=check&product_id={product}")
request_check = request.text
if "error" and "Invalid License Key" and "101" and "No matching license key exists" and "false" in request_check: # check what it returns
dosomething() # here you can print an error or do whatever you want
if "error" and "Invalid License Key" and "101" and "No matching license key exists"and "false" not in request_check: # If no error occured, activate the licence (you can choose how many time a licence can be activated on your woocommerce dashboard)
activation = wcapi.get(f"{website_url}/?wc-api=software-api&request=activation&email={licence_email}&request=activation&license_key={licence}&request=activation&product_id={product}")
activation # run the activation
activation_check = activation.text
if "activated" and ":true" and "0 out of 1 activations remaining" in activation_check: # check if licence is already activate (if it is then do something)
dosomething()
if "error" and "Exceeded maximum number of activations" and "103" and "Remaining activations is equal to zero" and "false" in activation_check: # check if licence exceeded the maximum number of activations
dosomething() # here you can print error or whatever you want
推荐阅读
- python - python中递归数据帧的数据分组方式
- jquery - 该脚本已在我的计算机上成功运行,但是当我将其发送给我的队友时,将无法获取任何数据
- javascript - 为什么不会通过在 sx 属性中将 display 设置为 none 来隐藏 MUI 组件?
- javascript - 括号在这个 javascript 表达式中的作用是什么?
- xcode - Xcode中奇怪的git diff log(完全是空格)
- c# - 如何在 C# 中生成确定性构建。如果我们在不同的目录/机器中有相同的代码
- python - 使用 Keras 的不平衡数据集分类
- java - 如何线程安全更新 loadingcache 值番石榴图
- java - Cucumber 5.5.0 我的步骤注释(之前、之后、之前...)不起作用
- distributed-system - 如何在同步环境中使异步消息传递快速可靠?