WordPress REST API 如何使用 Python 请求库为帖子分配不同的类别

问题描述 投票:0回答:3

我创建了一个 python 脚本来创建文章并将文章发布到 WordPress 网站,但似乎帖子数据中的类别没有分配给帖子,它总是分配给

uncategorized
类别,我只想分配帖子的 1 个类别。

我在这里做错了什么吗? WordPress REST API 文档并不是很有帮助。

这是帖子创建者功能:

# Post creator function
def create_post(inputTitleSent, outputText):
    randomAuthor = random.choice(authorList)
    
    post_status = "draft"
    headers = {
        "Content-Type": "application/x-www-form-urlencoded"
    }
    post = {
        "title": inputTitleSent,
        "content": outputText,
        "status": post_status,
        "author": randomAuthor,
        "categories:": "6"
    }
    url = wp_base_url + "/wp-json/wp/v2/posts"
    response = requests.post(url, data=post, headers=headers, auth=(wp_username,wp_password))
    return response

我已经尝试过

"categories": 6
,我在某处看到它应该是一个数组,所以我尝试了
"categories": [6]
"categories": ['6']
但仍然将帖子分配给
uncategorized
类别。

python wordpress python-requests wordpress-rest-api
3个回答
1
投票

刚刚遇到你的问题,有几件事引起了我的注意!

“我在某处看到它应该是一个数组,所以我尝试了“类别”:[6] 和“类别”:['6'],但仍然将帖子分配给未分类的类别。”

关于如何在 SO 上提出更好问题的一般友好提示:

  • 当您寻求帮助时,请向人们提供您在应用程序中遇到的确切错误/警告。这对于试图帮助您调试代码的人来说非常有帮助!结果,您会更快地解决问题。

回到您的代码,除了其他人已经提到的错字外,我认为

the bug is in your headers
。我过去通过以下步骤取得了成功:

1- 身份验证和适当的权限

在我们发送任何东西之前,我们需要确保我们可以使用 wordpress 验证自己。如果是这样,那么我们需要确保我们有适当的权限来创建帖子。由于您没有谈论身份验证/权限错误,那么我将假设您能够对自己进行身份验证并拥有创建帖子的适当权限,我们可以安全地继续下一步!

2-使用适当的
headers

确保设置正确的标题!由于我们要为 wordpress 提供一个

json
对象,因此我们的标题应包含以下内容:

headers = {"Content-Type": "application/json; charset=utf-8"}

3- 使用
json
对象作为您的发布数据/有效负载

我过去在

json
对象上取得过成功,所以我建议您使用
json
对象作为数据/有效负载。是的,类别应该是一个 id 数组。为此,您可以这样做:

post = {
  "title": inputTitleSent,
  "content": outputText,
  "status": post_status,
  "author": randomAuthor,
  "categories": [
    6,
    5
  ], 
}

4-将您的数据发布到
REST API

你可以使用这个:

response = requests.post(url, json=post, headers=headers, auth=(wp_username,wp_password))

或者这个:

response = requests.post(url, data=post, headers=headers, auth=(wp_username,wp_password))

理论上,两种方式都应该有效!


所以,你的整个代码将是这样的:

# Post creator function
def create_post(inputTitleSent, outputText):

    post_status    = "draft"
    randomAuthor   = random.choice(authorList)

    headers = {
      "Content-Type": "application/json; charset=utf-8"
    }

    post = {
      "title": inputTitleSent,
      "content": outputText,
      "status": post_status,
      "author": randomAuthor,
      "categories": [
        6,
        5
      ],
    }

    url = wp_base_url + "/wp-json/wp/v2/posts"

    response = requests.post(url, json=post, headers=headers, auth=(wp_username,wp_password))

    # or 
    # response = requests.post(url, data=post, headers=headers, auth=(wp_username,wp_password))
    
    return response

如果你能让它工作,请告诉我!


0
投票

所以,基本上,您的 POST 有效载荷中存在错字

"categories:": "6"
您只需从双引号中删除
:

def create_post(inputTitleSent, outputText):
    randomAuthor = random.choice(authorList)
    
    post_status = "draft"
    headers = {
        "Content-Type": "application/x-www-form-urlencoded"
    }
    post = {
        "title": inputTitleSent,
        "content": outputText,
        "status": post_status,
        "author": randomAuthor,
        "categories": [6]  # Replace "categories:" with "categories"
    }
    url = wp_base_url + "/wp-json/wp/v2/posts"
    response = requests.post(url, data=post, headers=headers, auth=(wp_username,wp_password))
    return response

0
投票

:
下的
post
有效负载中有一个额外的
categories
字符:

    post = {
        "title": inputTitleSent,
        "content": outputText,
        "status": post_status,
        "author": randomAuthor,
        "categories:": "6"
#                  ^
    }

只需删除它,您就会得到您想要的结果:

# Post creator function
def create_post(inputTitleSent, outputText):
    randomAuthor = random.choice(authorList)
    
    post_status = "draft"
    headers = {
        "Content-Type": "application/x-www-form-urlencoded"
    }
    post = {
        "title": inputTitleSent,
        "content": outputText,
        "status": post_status,
        "author": randomAuthor,
        "categories": "6"
    }
    url = wp_base_url + "/wp-json/wp/v2/posts"
    response = requests.post(url, data=post, headers=headers, auth=(wp_username,wp_password))
    return response
© www.soinside.com 2019 - 2024. All rights reserved.