Kristifer Szabo
Kristifer Szabo

Reputation: 569

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

I don't know why, but in order to use the API update to update the description of a video in your library you have to provide more than just the ID, you have to provide: the title, the ID, and the category ID. Why you need more than just the unique video ID is beyond me.

When I request a list of videos using the following parameters:

$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);

The returned list provides the Title and the ID but not the Category ID. Actually I don't know of any way of getting the category ID besides opening the video in your YouTube dashboard, looking at the category, and the comparing it to a list like this one

So, is their a way of updating a video's description without needing the category ID, or is there a way of acquiring that category ID using the API?

Here is how I am attempting to update the description.

// 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, can someone who has the right privilege add the youtube-php-api tag?

UPDATE: You cannot retrieve the Category ID using the SEARCH api, you must use the LIST api. What this means is that if you want to update the description of a video that you searched for, you need use 3 separate APIs. Here is how I used the LIST api to retrieve the category 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'];

Upvotes: 0

Views: 232

Answers (1)

stvar
stvar

Reputation: 6955

According to the docs, indeed you have to set categoryId for each video that you intend to update its snippet part.

Use VideoCategories endpoint to obtain a list of categories (along with corresponding IDs) available for your region.

Update:

If needing to get the category ID of a given (already existing) video, use the Videos endpoint, and lookup for the property items[].snippet.categoryId.

Upvotes: 1

Related Questions