WordPress API Authentication Error
-
Im trying to use the wordpress API to create a blog post on my site but when I run the below code I get a 401 user not able to create posts error even though I am the site admin.
import requests
import base64
wordpress_user = “Username”
wordpress_password = “Password”
wordpress_credentials = wordpress_user + “:” + wordpress_password
wordpress_token = base64.b64encode(wordpress_credentials.encode())
wordpress_header = {‘Authorization’: ‘Basic ‘ + wordpress_token.decode(‘utf-8’)}
def create_wordpress_post():
api_url = ‘https://kwonsoft.blog/wp-json/wp/v2/posts’
data = {
‘title’ : ‘Example wordpress post’,
‘status’: ‘draft’,
‘slug’ : ‘example-post’,
‘content’: ‘This is the content of the post’
}
response = requests.post(api_url,headers=wordpress_header, json=data)
print(response)
create_wordpress_post()
The blog I need help with is: (visible only to logged in users)
-
Hi there,
Although support staff is familiar with API useage we are not trained developers and will not be able to troubleshoot your code directly. However, its’ worth noting that sites on our managed hosting use a custom login system (for enhanced security) so it may be the case that if you are using the developer API here certain user commands will not work due to this difference. https://developer.wordpress.org/rest-api/
Instead you may wish to see if you can accomplish this using the wordpress.com-specific APIs here instead of the wordpress.org API: https://developer.wordpress.com/docs/api/
If you must use the wordpress.org API, you can turn off our secure login system by disabling SSO on your site. The steps to do this are found here.
Please note that the above instructions will walk you through how to create a wp-admin login for your site, which you will want to be sure to do before turning off SSO. Once SSO is inactive you will not connect to your site dashboard using your WordPress.com account, but by entering a second login that you set using the steps above.
Hope that helps. Please let us know if you have any more questions.
- The topic ‘WordPress API Authentication Error’ is closed to new replies.