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

huangapple go评论77阅读模式
英文:

WordPress REST API how to assign different categories to a post using Python requests library

问题

I've created a python script to create & post articles to WordPress site but it seems that the category in post data isn't getting assigned to the posts and it always gets assigned to uncategorized category, I am looking to only assign 1 category to the posts.

Am I doing something wrong here? WordPress REST API Docs aren't really helpful.

Here's the post creator function:

# 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

I've tried "categories": 6 and I saw somewhere that it's suppose to be an array so I tried "categories": [6] and "categories": ['6'] but still posts get assigned to uncategorized category.

英文:

I've created a python script to create & post articles to WordPress site but it seems that the category in post data isn't getting assigned to the posts and it always gets assigned to uncategorized category, I am looking to only assign 1 category to the posts.

Am I doing something wrong here? WordPress REST API Docs aren't really helpful.

Here's the post creator function:

# 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

I've tried "categories": 6 and I saw somewhere that it's suppose to be an array so I tried "categories": [6] and "categories": ['6'] but still posts get assigned to uncategorized category.

答案1

得分: 1

只是看到了你的问题,有几点引起了我的注意!

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

在SO上提问更好的方式的一般友好提示:

  • 当你寻求帮助时,请提供你的应用程序上出现的确切错误/警告。这对那些试图帮助你调试代码的人非常有帮助!这样,你的问题会更快得到解决。

回到你的代码,除了其他人已经提到的拼写错误之外,我认为**“bug在你的头部信息(headers)中”**。我在过去的经验中遇到过以下成功的步骤:

1- 认证和适当的权限

在我们发送任何请求之前,我们需要确保我们能够在WordPress上进行身份验证。如果可以的话,我们需要确保我们有适当的权限来创建帖子。由于你没有谈论认证/权限错误,我假设你能够进行身份验证并且有适当的权限来创建帖子,我们可以安全地进入下一步!

2- 使用适当的headers

确保设置适当的头部信息!由于我们将向WordPress提供一个json对象,所以我们的头部信息应该包含以下内容:

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

3- 将json对象作为你的帖子数据/payload

我在过去使用json对象时取得了成功,所以我建议你将json对象作为你的数据/payload。是的,类别(categories)应该是一个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))

理论上,两种方式都应该可行!


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

# 帖子创建函数
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))

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

请告诉我你是否成功了!

英文:

Just came across your question and couple of things caught my attention!

> "I saw somewhere that it's suppose to be an array so I tried "categories": [6] and "categories": ['6'] but still posts get assigned to uncategorized category."

General friendly tip on how to ask a better question here on SO:

  • When you reach out for help, please provide people with the exact error(s)/warning(s) that you got on your app. This would be very helpful for people who are trying to help you debug your code! As a result, you would get your problem solved faster.

Back to your code, In addition to the typo that others already mentioned, I think the bug is in your headers. I've had success with the following steps in the past:

1- Authentication and Proper Permissions

Before we send anything, we would need to make sure that we could authenticate ourselves with wordpress. If so, then we would need to make sure that we have proper permission(s) to create a post. Since you didn't talk about authentication/permissions errors, then I'm going to assume that you are able to authenticate yourself and have proper permissions to create a post and we are safe to move on to the next step!

2- Using proper headers

Make sure to set the proper headers! Since we're going to feed wordpress a json object, then our headers should contain this:

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

3- Using json object as your post data/payload

I've had success with json objects in the past, so I would suggest that you use json object as your data/payload. And yes, categories should be an array of ids. In order to do so, you could do this:

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

4- Posting your data to the REST API

You could either use this:

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

Or this:

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

Theoretically, both ways, should work!


So, your entire code would be something like this:

# 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

Let me know if you could get it to work!

答案2

得分: -1

So, Basically, there is a typo in your POST payload "categories:": "6" you can simply remove the : from the double quote

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
英文:

So, Basically, there is a typo in your POST payload "categories:": "6" you can simply remove the : from the double quote

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

答案3

得分: -1

You have an additional : character in the post payload under categories:

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

Simply remove this, and you'll get the results you're looking for:

# 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
英文:

You have an additional : character in the post payload under categories:

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

Simply remove this and you'll get the results you're looking for:

# 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

huangapple
  • 本文由 发表于 2023年4月17日 04:31:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定