使用python根据其id向worpaddress post添加标签时出错



你好,我正在尝试使用python根据其ID添加标签到wordpress帖子,但我一直得到这个错误:

```Error adding tags to post 540: {"code":"rest_invalid_param","message":"Invalid parameter(s): tags","data":{"status":400,"params":{"tags":"tags[0] is not of type integer."},"details":{"tags":{"code":"rest_invalid_type","message":"tags[0] is not of type integer.","data":{"param":"tags[0]"}}}}}```

这是我正在运行的代码,我在代码中提供了正确的Api密钥和wordpress url这只是为了测试的目的:

import requests

site_url = "https://set-services.shop"
# Replace YOUR_WORDPRESS_API_KEY with your WordPress API key
api_key = "T70A Bb2B HyF9 xS4L tzka R782"
# Set the number of posts to retrieve
posts_per_page = 100
# Set the page number to retrieve
page = 1
# Set the base URL for the WordPress API
api_url = f"{site_url}/wp-json/wp/v2/posts"
# Set the parameters for the API request
params = {
"per_page": 100
}
# Set the headers for the API request
headers = {
"Authorization": "Basic " +api_key
}
POST_ID = 540
#
# # Set the endpoint for updating the post
endpoint = site_url + f"/wp-json/wp/v2/posts/{POST_ID}"
#
# # Set the headers for the HTTP request
#
# # Set the list of tag names that you want to add to the post
tag_names = ["tag1", "tag2", "tag3"]
#
# # Set the payload for the HTTP request
# # In this case, we want to add the tags to the post
payload = {
"tags": tag_names
}
#
# # Send the HTTP request to update the post
response = requests.post(endpoint, headers=headers, json=payload)
#
# # If the request was successful
if response.status_code == 200:
#     # Print a success message
print(f"Successfully added tags to post {POST_ID}")
else:
#     # Print an error message
print(f"Error adding tags to post {POST_ID}: {response.text}")

我认为你必须使用标签id,而不是名称。标签必须是逗号分隔的字符串,而不是数组。(API参考在这里有点误导)

最新更新