通过 Python 脚本更改我的 WordPress 中的库存

问题描述 投票:0回答:1

我创建了一个代码,通过 Python 脚本更新 WordPress 中 1 个产品的库存,但出现了 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)

那么在这种情况下,使用你告诉我的内容,可能会这样吗?

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)
python wordpress inventory
1个回答
0
投票

这与您的用户/密码有关(403 duh)。我建议创建一个新用户并尝试找到正确的凭据。

我的网站之前也遇到过类似的问题,创建一个新用户并使用 API 插件有帮助

© www.soinside.com 2019 - 2024. All rights reserved.