luckyali444
luckyali444

Reputation: 296

YouTube - Data API v3 Unable to display private videos of my YouTube channel on my website using Google Service Account

I have few private videos on my YouTube channel and i want to put them on my website. Basically i don't want my website users to watch them directly on my YouTube channel. Furthermore i don't want to show consent screen to the users either. I browsed and found this example at stackoverflow in which DalmTo is suggesting someone to use Google Service Account to achieve this. My code works fine with publicly available videos but it does not show the private videos whenever i search the video with it's ID.

Here's my code:

<?php
session_start();
require_once('vendor/autoload.php');

$client = new Google_Client();
$client->setApplicationName("YouTube-Example");
$client->setScopes('https://www.googleapis.com/auth/youtube');

putenv("GOOGLE_APPLICATION_CREDENTIALS=service_account.json");
$client->useApplicationDefaultCredentials();

$youtube = new Google_Service_YouTube($client); 

try{
      $response = $youtube->videos->listVideos(
                  'snippet',
                  array(
                  'id' => 'pW0og1OZurE'    //private video unable to display
                  )); 

      echo "<pre>";
      print_r($response);

} catch (Google_Service_Exception $e) {
   $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
   htmlspecialchars($e->getMessage()));
} catch (Google_Exception $e) {
   $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
   htmlspecialchars($e->getMessage()));
}
?>

I followed the documentation but i can't figure out why it's not working. Please help me out.

Thanks in advance.

Upvotes: 0

Views: 2279

Answers (2)

arash andishgar
arash andishgar

Reputation: 11

Here may be your answer:

The forMine parameter indicates that the response should only search within the authorized user's videos. Also, since this request uses the forMine parameter, it must also set the type parameter value to video. If you have not uploaded any videos associated with that term, you will not see any items in the API response list.

Source: Google developers docs

Upvotes: 1

ReyAnthonyRenacia
ReyAnthonyRenacia

Reputation: 17613

Displaying of private videos is not allowed in Youtube API as stated in the help guide.

Private videos can only be seen by people who have been invited to view the video.

Here are some possible reasons why you or somebody you’ve shared the video with aren’t able to see a private video:

-Viewers need to have a YouTube account and be signed into it when trying to view the video.

-If the viewer has multiple YouTube accounts, they must be signed into the account which the video has been shared with.

-Since private videos don’t appear on a Channel page, the person will need to use the specific link to the private video. YouTube will send them an email with the link once you’ve invited them, but you can also send it yourself.

Upvotes: 0

Related Questions