使用Youtube API v3从Youtube频道中获取视频列表(没有恼人的身份验证弹出窗口)

使用Youtube API v3从Youtube频道中获取视频列表(没有恼人的身份验证弹出窗口)

问题描述:

I have a youtube channel and i'm using the Youtube API v3 to pull a list of videos uploaded in that channel (using the php libraries). The following is a snippet of the code i'm using :

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_YouTubeService.php';
session_start();

$OAUTH2_CLIENT_ID = 'xxxxxxxxx';
$OAUTH2_CLIENT_SECRET = 'xxxxxxxxx';

$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);

$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);

$youtube = new Google_YoutubeService($client);

$channelsResponse = $youtube->channels->listChannels('contentDetails', array(
      'mine' => 'true',

However, i get an annoying popup that asks me to login and authenticate in order to fetch the details. How can i get rid of this authentication popup? I'm planning to write a cron job that will pull the list of videos periodically and store it in a DB so i do not want that authentication popup.

NOTE : When i try to pull videos from a playlist, i'm not asked any authentication and the api functions smoothly

$playlistItemsResponse = $youtube->playlistItems->listPlaylistItems('snippet,status', array(
          'part' => 'snippet,contentDetails',
          'maxResults' => 50,
          'playlistId' => 'UUGpAMVStIfaQ32K-vhwNIxw'
        ));

Yes, you don't need OAuth2 login for that. You can simply do it by setting your API key instead.

It's a playlistItems->list request.

Here's demonstration in api explorer: https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.playlistItems.list?part=snippet&playlistId=PLjFEz-E0UPUxw3lFpnfV1dDA7OE7YIFRj&_h=2&

Instead of setting clientId and client Secret

set it's API key to your API key from cloud console in Public API access.

$client->setAPIKey($API_KEY);

You can get the video list from YouTube channel without authentication. Use YouTube Data API to fetch the list of the videos from YouTube channel.

$API_key    = 'Your_API_Key';
$channelID  = 'YouTube_Channel_ID';
$maxResults = 10;

$videoList = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelID.'&maxResults='.$maxResults.'&key='.$API_key.''));