I have a Wordpress site hosted by Bluehost, generic web store using WooCommerce and Elementor. I can add and edit products via the WooCommerce API without trouble, but whenever I try to post to the Wordpress REST API I get the error:
'{"code":"rest_cannot_create","message":"Sorry, you are not allowed to create posts as this user.","data":{"status":401}}'
My python code is:
import base64
import requests
def post_post(title, content):
url = 'https://mysite.com/wp-json/wp/v2/posts'
username = 'user'
password = 'applicationpassword'
credentials = username + ':' + password
token = base64.b64encode(credentials.encode())
authorization = 'Basic' + token.decode('utf-8')
post_data = {
'title': title,
'content': content
}
header = {
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36',
'Authorization': authorization
}
response = requests.post(url, headers=header, json=post_data)
return response
r = post_post("title", "content")
r.text
I have tried editing the .htaccess file as described in this topic but it did not work: authentication issue with rest api - rest_cannot_create
Any help would be greatly appreciated! I'm tearing my hair out here ;)