Basic authentication with Wordpress RESTAPI using python Wordpress pip package

admin

Administrator
Staff member
I am trying to create a basic authentication with WordPress RestAPI. I am trying to connect to a WordPress website hosted on localhost virtual Bitnami Environment.
I have installed an application password plugin to obtain an app password and trying to connect now with the <a href="https://pypi.org/project/wordpress-api/" rel="nofollow noreferrer">following python package</a> but for some reason when I print the response, I am getting 401.
Still, the JSON file is accessible from the web browser and shows the content.
Code:
print(&quot;Current Time =&quot;, current_date())

config = configparser.ConfigParser()
config.read('settings.ini')
url = config['WordpressRestAPI']['url']
user = config['WordpressRestAPI']['wpUser']
password = config['WordpressRestAPI']['wpPass']

credentials = user + ':' + password
token = base64.b64encode(credentials.encode())

header = {'Authorization': 'Basic' + token.decode('utf-8')}
post = {
    'title': 'Danny',
    'content': 'Hello World',
    'status': 'publish'
}

additional_metas = {'date_gmt': current_date(),
                    'slug': 'Danny',
                    'author': 2,
                    'featured_media': 41,
                    'type': 'influencer',
                    'comment_status': 'closed',
                    'ping_status': 'open',
                    'format': 'standard'}
post.update(additional_metas)
post_type_name = config['WordpressRestAPI']['postTypeName']
r = requests.post(url + post_type_name, headers=header, json=post)
print(r)
This is the configuration I made:
Code:
    url = http://192.1.1.1/wp-json/wp/v2/
    wpUser = abc
    wpPass = aaaa bbbb cccc dddd eeee ffff
    postTypeName = influencers