oAuth1.0a不适用于使用请求的Python的WordPress API,尽管调用来自Postman的工作



我对编程和Python相当陌生,我尝试使用OAuth1.0a发布到wordpress REST API,以在我的博客上创建帖子。在邮递员中,一切都运行良好,因此凭据还可以。但是我下面的 Python 代码返回 401 - rest_cannot_create。我已经尝试了 5 个小时了。希望您能帮助新手!

谢谢!

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))

我不得不用以下内容替换最后两行,它对我有用 -

r = requests.post(url, auth=auth, data=post)   
  print(r.content)

最新更新