How to setup & use Wordpress REST API authentication using Python

Deutsch English

Wordpress authentication plugin setup

First, install the WordPress REST API Authentication wordpress plugin, which you can find by searching for WordPress REST API Authentication:

WordPress plugin search results showing WordPress REST API Authentication plugin

Then you need to open the plugin configuration page. Open Plugins in the Wordpress admin panel, locate the WordPress REST API Authentication plugin and click Configure WordPress plugins page with Configure button for REST API Authentication plugin

Select Basic Authentication:

WordPress REST API Authentication plugin configuration selecting Basic Authentication

Then click Next on the top right:

WordPress REST API Authentication plugin configuration Next button

and click Finish on the next page:

WordPress REST API Authentication plugin configuration Finish button

Setup in Python

Assuming you have a Wordpress user admin with password abc123 we can modify our code from How to get WordPress posts as JSON using Python & the WordPress REST API in order to query a non-public endpoint:

wp_rest_auth_example.py
import requests
import base64

# Compute basic authentication header
auth_header = b"Basic " + base64.b64encode(b"admin:abc123")

# posts is a list of JSON objects, each representing a post
posts = requests.get("https://mydomain.com/wp-json/wp/v2/posts",
                     params={"context": "edit"},
                     headers={"Authorization": auth_header}).json()

Check out similar posts by category: Python, Wordpress