pythonyoutubeyoutube-apiyoutube-data-api

Youtube Data API Invalid Category ID


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?


Solution

  • 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.