rest_cannot_create Sorry, you are not allowed to create posts as this user in WordPress REST API
The issue
When we were working with WordPress REST API, we got into this issue. We were trying to create a post in WordPress using WordPress post API (
{ "code": "rest_cannot_create", "message": "Sorry, you are not allowed to create posts as this user.", "data": { "status": 401 } }
Reasons
This can happen for multiple reasons.
- In our case this happend because the very first attempted we tried the post request without any authentication.
- Then we tried the post request with a user credential who didn’t have access
Solution
We had to use a JWT plugin to generate an authentication token. Following is a plugin that we can use to generate the authentication token:
https://wordpress.org/plugins/jwt-authentication-for-wp-rest-api/
Once we got the token we call the post request like below and it solved the issue:
create_post_url = WP_API_ENDPOINT + "/wp/v2/posts" headers = {'content-type': 'application/json', 'Authorization': "Bearer " + wordpress_token}
Read more about WordPress REST API:
Share