在 YouTube PHP API 中使用 listSearch() 函数时是否可以 return 视频的类别 ID?

Is it possible to return the category ID of a video when using the listSearch() function in the YouTube PHP API?

我不知道为什么,但要使用 API 更新来更新库中视频的描述,您不仅需要提供 ID,还必须提供:标题、ID 和类别 ID。为什么你需要的不仅仅是唯一的视频 ID 不明白。

当我使用以下参数请求视频列表时:

$queryParams = [
        'forMine' => true,
        'q' => $q,
         // 'fields' => 'items(id,snippet/title,snippet/description)', <- would like to specify categoryId here
        'type' => 'video',
        'maxResults' => 50
    ];
$response = $service->search->listSearch('snippet', $queryParams);

返回的列表提供标题和 ID,但不提供类别 ID。实际上,除了在 YouTube 控制面板中打开视频、查看类别并将其与这样的列表进行比较之外,我不知道有什么方法可以获取类别 ID

那么,他们是一种无需类别 ID 即可更新视频描述的方法,还是可以使用 API 获取该类别 ID?

这是我尝试更新描述的方式。

// Auth the client
$service = new Google_Service_YouTube($client);

// Define the $video object, which will be uploaded as the request body.
$video = new Google_Service_YouTube_Video();
// Set the ID
$video->setId($video_id);

// Add 'snippet' object to the $video object.
$videoSnippet = new Google_Service_YouTube_VideoSnippet();
$videoSnippet->setCategoryId('<HOW TO GET THIS?>');
$videoSnippet->setDescription('Test Description');
$videoSnippet->setTitle($video_title);
$video->setSnippet($videoSnippet);

$response = $service->videos->update('snippet', $video);

PS,有权限的人可以添加youtube-php-api标签吗?

更新: 您无法使用 SEARCH api 检索类别 ID,您必须使用 LIST api。这意味着如果你想更新你搜索的视频的描述,你需要使用 3 个单独的 APIs。以下是我如何使用 LIST api 检索类别 ID:

    $client = google_authenticate();
    $service = new Google_Service_YouTube($client);
    $queryParams = [
        'id' => $video_id,
        'fields'=>'items(snippet/categoryId)'
    ];

    $response = $service->videos->listVideos('snippet', $queryParams);

    return $response['items'][0]['snippet']['categoryId'];

根据 the docs,您确实必须为要更新其 snippet 部分的每个视频设置 categoryId

使用 VideoCategories endpoint 获取适用于您所在地区的类别列表(以及相应的 ID)。

更新:

如果需要获取给定(已存在)视频的类别 ID,请使用 Videos endpoint, and lookup for the property items[].snippet.categoryId