最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Youtube Data API Invalid Category ID - Stack Overflow

programmeradmin0浏览0评论

I am currently creating an application in python which involves the use of the Youtube Data API (v3). However, I have been receiving an error regarding the category ID. This is the code:

import os
from subprocess import run
os.chdir(os.path.dirname(__file__))

video_name = "video1010.mp4"
title = "This is a test"
vid_des = "This is a test"
keywords = "this, is, a, test"
category = "42"

command = f'python3 uploadYoutube.py --file="{video_name}" --title="{title}" --description="{vid_des}" --keywords="{keywords}" --category="{category}" --privacyStatus="public"'

terminal_output = run(command, capture_output=True).stdout

print(terminal_output)

The output I get is (shortened):

An HTTP error 400 occurred:\r\nb\'{\\n  "error": {\\n    "code": 400,\\n    "message"
: "The \\\\u003ccode\\\\u003esnippet.categoryId\\\\u003c/code\\\\u003e property specif
ies an invalid category ID.

I have used Curl to obtain the category IDs for this API, and 42 appears to be valid:

    {
      "kind": "youtube#videoCategory",
      "etag": "TxVSfGoUyT7CJ7h7ebjg4vhIt6g",
      "id": "42",
      "snippet": {
        "title": "Shorts",
        "assignable": false,
        "channelId": "UCBR8-60-B28hp2BmDPdntcQ"
      }
    },

Is this caused by the "assignable":false line, or is it something else?

I am currently creating an application in python which involves the use of the Youtube Data API (v3). However, I have been receiving an error regarding the category ID. This is the code:

import os
from subprocess import run
os.chdir(os.path.dirname(__file__))

video_name = "video1010.mp4"
title = "This is a test"
vid_des = "This is a test"
keywords = "this, is, a, test"
category = "42"

command = f'python3 uploadYoutube.py --file="{video_name}" --title="{title}" --description="{vid_des}" --keywords="{keywords}" --category="{category}" --privacyStatus="public"'

terminal_output = run(command, capture_output=True).stdout

print(terminal_output)

The output I get is (shortened):

An HTTP error 400 occurred:\r\nb\'{\\n  "error": {\\n    "code": 400,\\n    "message"
: "The \\\\u003ccode\\\\u003esnippet.categoryId\\\\u003c/code\\\\u003e property specif
ies an invalid category ID.

I have used Curl to obtain the category IDs for this API, and 42 appears to be valid:

    {
      "kind": "youtube#videoCategory",
      "etag": "TxVSfGoUyT7CJ7h7ebjg4vhIt6g",
      "id": "42",
      "snippet": {
        "title": "Shorts",
        "assignable": false,
        "channelId": "UCBR8-60-B28hp2BmDPdntcQ"
      }
    },

Is this caused by the "assignable":false line, or is it something else?

Share asked Mar 3 at 20:06 Zak VirgoZak Virgo 775 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You're correct! It's because of the key "assignable" being false for the category ID 42 which is for YouTube shorts.

Using this documentation I used the following code and got the same information as you.

request = youtube_client.videoCategories().list(
    part="snippet",
    regionCode="US",
)
response = request.execute()
print(response)

So if I were you I'd choose another category unless you absolutely wanted to create a video with a category ID of 42.

Maybe refer to these answers for creating a YouTube short via the API.

发布评论

评论列表(0)

  1. 暂无评论