问题在于如何处理您的身份验证
requests.post
电话。使用WordPress REST API处理身份验证有两种主要方法。
当您使用auth参数时,您正在使用基本身份验证,这就是它工作的原因,请检查您的代码:
response = requests.post(url, auth=(username, password), json=data)
.
尽管auth参数在内部为Basic Athentication设置了正确的Authorization标头,但在直接使用标头时,您需要正确格式化Authorization标头。您使用的标头字典的格式不正确,无法进行基本身份验证。所以,你需要设置
Authorization
带有base64编码字符串的标头
username:password
.
试试这个:
import requests
import base64
# PAGE WHERE I WANT TO SEND INFO
url = "https://dca-mx.com/wp-json/wp/v2/pages/362"
username = 'Fabian'
# The application password you generated
password = 'XXXX XXXX XXXX XXXX XXXX'
# The post data
data = {
'title': 'DCA DB',
'content': 'HELLO FROM PYTHON.',
'status': 'publish'
}
# Encode the username and password
credentials = f"{username}:{password}"
encoded_credentials = base64.b64encode(credentials.encode('utf-8')).decode('utf-8')
header = {"Authorization": f"Basic {encoded_credentials}"}
# Send the HTTP request (it should still work)
response = requests.post(url, headers=header, json=data)
# Now, check the response
if response.status_code == 201:
print('Post created successfully')
elif response.status_code == 200:
print('Posteado Correctamente!')
else:
print(response.text)
希望它能帮助我,让我更新!