尝试通过Rest API在安装在Hostgator网站上的Wordpress博客上发布博客文章,它被Mod_Security阻止

t98cgbkg  于 12个月前  发布在  WordPress
关注(0)|答案(1)|浏览(110)

下面是我使用的Python脚本:

import requests
from requests.auth import HTTPBasicAuth

url = 'https://my-phenomenal-website.com/blog/wp-json/wp/v2/posts'
data = {
    'title': 'TEST POST 1.1',
    'content': 'My outstanding content would go RIGHT HERE',
    'status': 'publish'
}

# Replace 'your_username' with the WordPress username
# Replace 'your_application_password' with the application password you created
auth = HTTPBasicAuth('USERNAME IS HERE', 'PASSWORD GOES HERE')

response = requests.post(url, json=data, auth=auth)

if response.status_code == 201:
    print("Post published successfully.")
else:
    print("Failed to publish post:", response.content)

字符串
我尝试了管理员用户名,以及另一个用户名,使用我为每个用户创建的应用程序密码.没有运气.响应如下:

Failed to publish post: b'<head><title>Not Acceptable!</title></head><body><h1>Not Acceptable!</h1><p>An appropriate representation of the requested resource could not be found on this server. This error was generated by Mod_Security.</p></body></html>'


尝试修改.htaccess文件,无论是在根域,以及/博客子域,以禁用国防部的安全,因为许多建议来解决这个问题。没有运气。仍然得到相同的错误。

<IfModule mod_security.c>
  SecFilterEngine Off
  SecFilterScanPOST Off
</IfModule>


其他的想法,我可以如何解决这个问题?

s6fujrry

s6fujrry1#

我花了几个小时试图解决这个问题,以下是对我有效的方法:

import requests
import json  # Import for json.dumps

ROOT = 'https://your-website.com/blog/'
AUTH_USER = 'Your Wordpress Username'
AUTH_PASSWORD = 'Your password, created using the Application Passwords section under the above user in the Wordpress Users section'

headers = {
    'Content-Type': 'application/json',
    'User-Agent': 'My Python WordPress API Script'  # Anything will work here apparently, just important that BOTH of these are included.
}

data = {
    'title': 'Test WP API',
    'status': 'publish',
    'slug': 'test-wp-api',
    'author': 1,
    'content': 'This is my first post created using REST-API' # include ALL of these parameters for it to work properly
}

# Convert data to JSON format
data_json = json.dumps(data)

response = requests.post(url=ROOT + 'wp-json/wp/v2/posts', data=data_json, headers=headers, auth=(AUTH_USER, AUTH_PASSWORD))

if response.status_code == 201:
    print("Post published successfully.")
else:
    print("Failed to publish post:", response.content)

字符串

相关问题