首页 > 解决方案 > 如何使用 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}}

标签: python-3.xwoocommercewoocommerce-rest-apiwoocommerce-subscriptions

解决方案


这是如何做到的:

在开始之前,如果您还没有安装下面的库,则需要安装。您可以像这样使用 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

推荐阅读