使用Python脚本更新WordPress库存遇403错误求助
WordPress商品库存更新403错误排查与修复
我写了两段Python脚本尝试更新WordPress单个商品的库存,但均返回403响应错误,附上代码寻求解决办法:
第一段代码
import requests import operator import json import csv import os import re import pandas as pd import base64 user = ('XXX') password = ('XXX') creds = user + ':' + password token = base64.b64encode(creds.encode()) headers = { "Authorization": f"Bearer {token}" } # header = {'Authorization': 'Basic ' + token.decode('utf-8')} post = { 'Lagerbestand': 2 #Inventory } url_base = 'https://example.com/product/product_name' r = requests.post(url_base, headers, json=post) print(r)
第二段代码(使用WooCommerce REST API)
import requests # Set-Up credentials of REST API of WordPress url = 'https://example.com/wp-json/wc/v3/products/{product_id}' consumer_key = 'customer_key' consumer_secret = 'customer_secret' # ID product to update product_id = 123 # New details of the product new_inventory = 50 # Configura los parámetros de autenticación auth = (consumer_key, consumer_secret) # PUT call to update the inventaroy response = requests.put(url.format(product_id=product_id), auth=auth, json={'stock_quantity': new_inventory}) # Checked if response.status_code == 200: print('Done') else: print('Mistake:', response.text)
第一段代码的核心问题
- 认证前缀错误:WordPress/WooCommerce的Basic认证需要用
Basic前缀,不是Bearer,你注释掉的那行才是正确写法,当前用Bearer会直接导致认证失败。 - 请求方法错误:更新商品数据应该用
PUT请求,POST是用于创建新资源的。 - 接口地址错误:直接调用商品的前端URL(
https://example.com/product/product_name)不是API接口,必须使用WooCommerce标准API端点/wp-json/wc/v3/products/{product_id}。 - 字段名错误:
Lagerbestand不是WooCommerce API的标准库存字段,正确字段是stock_quantity;如果商品之前未启用库存管理,还需要添加manage_stock: true参数。
修正后的第一段代码示例:
import requests import base64 # 替换为你的WooCommerce Consumer Key和Secret consumer_key = 'XXX' consumer_secret = 'XXX' creds = f"{consumer_key}:{consumer_secret}" token = base64.b64encode(creds.encode()).decode('utf-8') headers = { "Authorization": f"Basic {token}" } payload = { 'stock_quantity': 2, 'manage_stock': True # 若商品未开启库存管理,必须添加此参数 } # 替换为目标商品的ID product_id = 123 url = f'https://example.com/wp-json/wc/v3/products/{product_id}' response = requests.put(url, headers=headers, json=payload) print(response.status_code) print(response.text)
第二段代码的潜在问题
这段代码的思路是对的,但403错误可能来自以下几点:
- 密钥权限不足:在WooCommerce后台生成Consumer Key时,必须勾选读/写权限,仅读权限无法修改商品数据。
- 安全插件拦截:Wordfence、iThemes Security等安全插件可能会拦截API请求,可暂时禁用插件测试,若恢复正常则在插件中添加允许当前IP访问API的规则。
- 服务器防火墙限制:部分主机商的防火墙会拦截非浏览器发起的API请求,可联系主机商排查。
- SSL验证问题:若服务器SSL证书配置异常,可尝试添加
verify=False参数测试(仅用于排查,生产环境请勿使用):response = requests.put(url.format(product_id=product_id), auth=auth, json={'stock_quantity': new_inventory}, verify=False) - 商品状态限制:仅发布状态的商品可通过API修改,且需确保商品已启用库存管理(或在payload中添加
manage_stock: true)。
通用排查步骤
- 用curl工具直接测试API,排除代码问题:
若curl也返回403,说明问题出在服务器配置或权限,而非代码。curl -X PUT https://example.com/wp-json/wc/v3/products/123 -u YOUR_CONSUMER_KEY:YOUR_CONSUMER_SECRET -d '{"stock_quantity": 50}' - 登录WooCommerce后台,确认密钥权限和商品状态:
- 路径:WooCommerce → 设置 → 高级 → REST API,检查对应密钥的权限。
- 查看目标商品,确认已启用库存管理且状态为发布。
- 检查服务器日志:查看WordPress或服务器的错误日志,获取403错误的具体原因。
内容的提问来源于stack exchange,提问作者Manuel Guerrero
相关产品推荐
相关产品推荐

