Zenth
Zenth

Reputation: 811

Use the wordpress OAuth from PHP without user prompt from credentials, and make API calls

I have a Wordpress project with the OAuth Server, jSON API User and jSON API plugins enabled, and i get the ID, Key and Secret string using:

php /var/www/mywordpress/wp-cli.phar --url=mydomain.com oauth1 add

Now I need to create/edit/delete users from OUTSIDE Wordpress (from PHP Project).

I read more tutorials to use the OAuth1 to get access token and can call the API with permissions, but the codes allways prompt to the user the login window to get the access permissions.

I need to make all this by the server side, using only PHP (CURL or similar) to get the credentials and make calls to the API.

Any know how, or have any code example to login into wordpress OAuth from pure PHP ?

Thanks

Upvotes: 1

Views: 908

Answers (1)

svershin
svershin

Reputation: 58

I was facing a similar issue and solved it with the following approach:

  1. Use Wordpress OAuth Server plug-in instead of the one suggested by the WP REST API docs.
  2. Enable "User Credentials" grant type in the settings.
  3. Follow the directions for using the Postman tool to get the idea of the flow for obtaining the OAuth access token.

I don't have a PHP code sample as I'm working in JavaScript. However, the steps are fairly easy:

  • Issue a POST request to yourserver.com?oauth=token (step 3 in the list above)
  • Append ?access_token=xxxxxx (xxxxxx being the access token) to all subsequent requests to the API endpoints that require privileged access.

Upvotes: 1

Related Questions