1

I am fairly new to programming and Python and I tried to POST to the wordpress REST API with OAuth1.0a to create a post to my blog. In Postman it all works well, so the credentials are ok. But my Python code below returns 401 - rest_cannot_create. I have been trying to make this work for 5 hours now. I hope you can help a newbie!

Thank you!

import requests
from requests_oauthlib import OAuth1
import json

url = "https://example.com/wp-json/wp/v2/posts/"
oauth_consumer_key = "1234567"
oauth_consumer_secret = "ABCDEFG"
oauth_token = "9876543"
oauth_token_secret = "HIJKLMNOP"

auth = OAuth1(oauth_consumer_key, oauth_consumer_secret, oauth_token, oauth_token_secret)

post = {'date': '2017-06-19T20:00:35',
        'title': 'Testing API via Python',
        'slug': 'rest-api-1',
        'status': 'draft',
        'content': 'this is the content post',
        'excerpt': 'this is the excerpt',
        'format': 'standard',
        'author': "1"
        }

r = requests.post(url, auth=auth, json=post)
print(json.loads(r.content))
tobsen
  • 11
  • 2

1 Answers1

0

I had to replace the last two lines with the following and it worked for me -

r = requests.post(url, auth=auth, data=post)   
  print(r.content)
Derrick
  • 3,669
  • 5
  • 35
  • 50