I created a code to update the stock of 1 product in my WordPress by Python script but appear a Response 403 and I don´t know why. I let here the code to know if someone can help me on it.
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)
The next code is using REST API for Wordpress but anyway doesnt work.
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)
This has to do with your
user/password(403 duh). I recommend creating a new user and trying to find the correct credentials.I have had a similar problem before with my website and creating a new user and using API plugins has helped