I'm building a mobile app for a WP site and I'm trying to get POST requests to the WP-API working, starting with users.
I'm following the process outlined in a few tutorials:
- Install Basic Auth plugin
- In Postman, prepare a request to
POST wp-json/wp/v2/users, filling in credentials for my admin user, using Basic Auth, in the authentication tab. - In the body, include a username, email, and password.
(nothing else, no other headers or anything)
And I get a 401 error:
{
"code": "rest_cannot_create_user",
"message": "Sorry, you are not allowed to create new users.",
"data": {
"status": 401
}
}
A questions here mention cookies and being signed in or out in the browser; I get the same result whether or not I'm signed in to my WP Admin console in Chrome. I'm using the Postman desktop client.
(I'm off to try to figure out oAuth or JWT, but still perplexed why this more basic route isn't working!)
UPDATE
I've followed these steps from an OAuth tutorial and gotten the same 401 response (after attempting a DELETE request)
- Install and activate OAuth1 plugin
- Register a new application, to get client key and client secret. (Pointing the callback url to a simple placeholder file I put on my server)
POST /oauth1/requestwith step 2's key and secret asoauth_consumer_keyandoauth_consumer_secret(oauth_signature_method= HMAC-SHA1), passed as auth headers. Receiveoauth_tokenandoauth_secret.- Visit
http://my-dev-server.com/oauth1/authorize?oauth_token=<token_here>&oauth_token_secret=<secret_here>in my browser, withoauth_tokenandoauth_secretfrom step 3. - Brought to the WP connect page (logged in as the admin for the site), click Authorize. Receive
verification token. POST /oauth1/access, withoauth_consumer_keyandoauth_consumer_secretused in step 3 andoauth_tokenandoauth_secretreceived in step 3 as auth headers, and with the verification token from step 5 as query in URLoauth_verifier(http://my-dev-server/oauth1/access?oauth_verifier=<oauth_verifier_value>). receive newoauth_tokenandoauth_secret.DELETE /wp-json/wp/v2/job-listings/936(the id of a listing I just created, from the website, to be deleted) using the originaloauth_consumer_keyandoauth_consumer_secretand the newoauth_tokenandoauth_secret(received in step 6) as auth headers.
And then I get the 401 response. Note, of course, that all intermediate steps appear to have worked correctly. I never replaced my placeholder callback, but the tutorial didn't say anything about that. But maybe that's my problem?
I'll also note that I haven't done any server setup to expose positing to the API. None of these tutorials, as far as I can tell, mention any pre-work like this so I assumed it would just work out of the box. (someone else designed the WP site and picked all the database plugins etc).