Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I attempt to publish a post to WordPress blog with python demands and rest programming interface by the following code: 

auth = 'Basic ' + str(base64.b64encode(b'admin:123456'), 'utf-8')

headers = {'Authorization': auth}

body = {'title': 'Hello World'}

r = requests.post('wp-json/wp/v2/posts', headers=headers, data=body)

and I consistently got 401 error: 

>>> r.text

'{"code":"rest_cannot_create","message":"Sorry, you are not allowed to create posts as this user.","data":{"status":401}}'

I'm almost certain that the account administrator and password are right and have an overseer role, Did I miss anything?

1 Answer

0 votes
by (26.4k points)

Check out the following steps:

  1. Try to install and activate the plugins on the wordpress
  2. Follow the assistance text there and make your password string for your userid - expect it is mypassword123
  3. Now, try this on your terminal

"admin:mypassword123" | base64

      4. Then, You will get new password

Check out the below code:

url_srcdest = "http://example.com/wp-json/wp/v2/pages/"

headers = {'Content-Type': 'application/json', 

         'Authorization': 'Basic pwdabc123',

         'Username': '<your username>', 

         'Password':'pwdabc123'}

data = \

    {

        "title":"Testing via API via Python",

        "content":"tEST CONTENT OF THE THIS TEST PAGE via PYTHON",

        "status": "publish"

    }

response = requests.post(url_srcdest, data=json.dumps(data), headers=headers)

Join this python online course fast, to learn python concepts in detail and get certified.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Aug 6, 2019 in Web Technology by Sammy (47.6k points)

Browse Categories

...