Python WordPress Rest API Post 请求返回 406 错误
尝试使用以下 python 脚本发布时,WordPress Rest API 返回 406 错误。我已将 JSON 发布数据精简到最低限度,并使用 WordPress 核心应用程序密码生成器和多个不同的用户帐户,所有这些帐户都具有管理员权限。我尝试使用包含空格和删除空格的密码,但无济于事。我遵循 YouTube 上的教程,对我的脚本进行了建模,以匹配导师的脚本,无论是使用 https 还是不使用 SSL。使用http,也无济于事。我非常感谢任何解决此问题的帮助:这是我使用的脚本:
import os
import json
import requests
import base64
url = 'https://domainname.com/wp-json/wp/v2'
user = 'AdministratorUserName'
password = '0123 4567 8901 2345 6789 0123'
credentials = user + ':' + password
token = base64.b64encode(credentials.encode())
header = {'Authorization': 'Basic ' + token.decode('utf-8')}
post = {
'date': '2022-03-23T10:00:00',
'title': 'testing',
'content': 'testing testing 123',
'status': 'publish'
}
wordPressresponse = requests.post(url + '/posts', headers=header, json=post)
with open('wordPresResponseLog.txt', 'a') as wPResponse:
wPResponse.write(str(wordPressresponse))
The WordPress Rest API returns a 406 error when attempting to post with the following python script. I've stripped the JSON post data down to a bare minimum and used the WordPress core application password generator with several different user accounts all which have administrator privileges. I've tried using the password with the spaces included and with them removed as well to no avail. I've followed a tutorial on YouTube and modeled my script to match the tutor's with both using https for SSL and without. using http, also to no avail. I would really appreciate any help to resolve this issue: Here's the script I used:
import os
import json
import requests
import base64
url = 'https://domainname.com/wp-json/wp/v2'
user = 'AdministratorUserName'
password = '0123 4567 8901 2345 6789 0123'
credentials = user + ':' + password
token = base64.b64encode(credentials.encode())
header = {'Authorization': 'Basic ' + token.decode('utf-8')}
post = {
'date': '2022-03-23T10:00:00',
'title': 'testing',
'content': 'testing testing 123',
'status': 'publish'
}
wordPressresponse = requests.post(url + '/posts', headers=header, json=post)
with open('wordPresResponseLog.txt', 'a') as wPResponse:
wPResponse.write(str(wordPressresponse))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够通过将以下行添加到标题来解决该问题:
I was able to resolve the issue by adding the following line to the header: