Python WordPress Rest API Post 请求返回 406 错误

发布于 2025-01-16 21:13:16 字数 900 浏览 1 评论 0原文

尝试使用以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原来分手还会想你 2025-01-23 21:13:16

我能够通过将以下行添加到标题来解决该问题:

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36'

I was able to resolve the issue by adding the following line to the header:

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文