Youtube API 获取 Livestream scheduledStartTime
Youtube API get Livestream scheduledStartTime
我需要从我的 Youtube 频道获取即将到来的直播列表,并将它们显示给我的网站访问者。
问题是使用简单的 API 关键 http 请求 ('https://youtube.googleapis.com/youtube/v3/') 我无法访问 scheduledStartTime 值。
这样做的唯一方法是使用 oauth 身份验证,因此请求将是
'使用令牌获取 https://www.googleapis.com/youtube/v3/liveBroadcasts'。
这是我用于基本 API 密钥请求的代码:
<?php
$base_url = 'https://youtube.googleapis.com/youtube/v3/';
$key = 'API KEY';
$channelId = 'CHANNEL ID';
$maxResults = 30;
$API_URL_LIVE_UPCOMING = $base_url . 'search?order=date&part=snippet&channelId=' .$channelId. '&maxResults=' .$maxResults. '&eventType=upcoming&type=video&key=' .$key;
$url_upcoming = $API_URL_LIVE_UPCOMING;
$json = file_put_contents('youtube-cache-upcoming.json', file_get_contents($url_upcoming));
$json_data = file_get_contents('youtube-cache-upcoming.json');
foreach ( $json_data->items as $item ) {
$id = $item->id->videoId;
$title = $item->snippet->title;
$publishTime = $item->snippet->publishTime;
$description = $item->snippet->description;
$thumbnail = $item->snippet->thumbnails;
$mediumThumbnail = $thumbnail->medium->url;
echo '<div class="16022021" style="border-bottom: 1px solid #647279; margin-bottom:4%;margin-top:4%;padding-bottom: 30px; width: 100%;">';
echo '<div class="col-md-7">';
//echo '<p style="font-size:1.2em;">'.$publishTime.'</p>';
echo '<p style="font-size:1.4em;color:#FF714D"><strong>'.$title.'</strong></p>';
echo '<p style="font-size:1.2em;">'.$description. '</p>';
echo '<p style="margin-top:4%;"><a class="btn btn-lg btn-success" href="https://www.youtube.com/watch?v='.$id.'">VIEW VIDEO</a></p>';
echo '</div>';
echo '<div class="col-md-5 align-left"><iframe width="560" height="315" src="https://www.youtube.com/embed/'.$id.'?rel=0&autoplay=1" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>';
//echo '<div class="col-md-5 align-left"><a target="_blank" href="https://www.youtube.com/watch?v='.$id.'"><img class="img-responsive" alt="'.$title.'" src="'.$mediumThumbnail.'" title="'.$title.'" /></a></div>';
echo '</div>';
}
echo '</div>';
?>
我基本上想获得类似的简单结果,很高兴看到有关如何管理身份验证过程并在 html 页面上显示响应的编码示例,而无需安装任何库。
感谢您的帮助。
这里是 youtube.liveBroadcasts.list 方法的一个基本示例。
<?php
/**
* Sample PHP code for youtube.liveBroadcasts.list
* See instructions for running these code samples locally:
* https://developers.google.com/explorer-help/guides/code_samples#php
*/
if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
throw new Exception(sprintf('Please run "composer require google/apiclient:~2.0" in "%s"', __DIR__));
}
require_once __DIR__ . '/vendor/autoload.php';
$client = new Google_Client();
$client->setApplicationName('API code samples');
$client->setScopes([
'https://www.googleapis.com/auth/youtube.readonly',
]);
// TODO: For this request to work, you must replace
// "YOUR_CLIENT_SECRET_FILE.json" with a pointer to your
// client_secret.json file. For more information, see
// https://cloud.google.com/iam/docs/creating-managing-service-account-keys
$client->setAuthConfig('YOUR_CLIENT_SECRET_FILE.json');
$client->setAccessType('offline');
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open this link in your browser:\n%s\n", $authUrl);
print('Enter verification code: ');
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
// Define service object for making API requests.
$service = new Google_Service_YouTube($client);
$queryParams = [
'id' => 'YOUR_BROADCAST_ID'
];
$response = $service->liveBroadcasts->listLiveBroadcasts('snippet,contentDetails,status', $queryParams);
print_r($response);
我已经设法以不同的方式获得了 scheduledStartTime 值,没有 Oauth2。
基本上,我使用了两个 api 调用:
第一个是
https://youtube.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='
.$channelId. '&maxResults=' .$maxResults.
'&eventType=upcoming&type=video&key=' .$key;
然后对于任何视频 ID,我调用视频端点:
https://youtube.googleapis.com/youtube/v3/videos?part=liveStreamingDetails&id='
.$id. '&key=' .$key;
在这里我可以提取任何视频的 scheduledStartTime 值。
我需要从我的 Youtube 频道获取即将到来的直播列表,并将它们显示给我的网站访问者。
问题是使用简单的 API 关键 http 请求 ('https://youtube.googleapis.com/youtube/v3/') 我无法访问 scheduledStartTime 值。
这样做的唯一方法是使用 oauth 身份验证,因此请求将是 '使用令牌获取 https://www.googleapis.com/youtube/v3/liveBroadcasts'。
这是我用于基本 API 密钥请求的代码:
<?php
$base_url = 'https://youtube.googleapis.com/youtube/v3/';
$key = 'API KEY';
$channelId = 'CHANNEL ID';
$maxResults = 30;
$API_URL_LIVE_UPCOMING = $base_url . 'search?order=date&part=snippet&channelId=' .$channelId. '&maxResults=' .$maxResults. '&eventType=upcoming&type=video&key=' .$key;
$url_upcoming = $API_URL_LIVE_UPCOMING;
$json = file_put_contents('youtube-cache-upcoming.json', file_get_contents($url_upcoming));
$json_data = file_get_contents('youtube-cache-upcoming.json');
foreach ( $json_data->items as $item ) {
$id = $item->id->videoId;
$title = $item->snippet->title;
$publishTime = $item->snippet->publishTime;
$description = $item->snippet->description;
$thumbnail = $item->snippet->thumbnails;
$mediumThumbnail = $thumbnail->medium->url;
echo '<div class="16022021" style="border-bottom: 1px solid #647279; margin-bottom:4%;margin-top:4%;padding-bottom: 30px; width: 100%;">';
echo '<div class="col-md-7">';
//echo '<p style="font-size:1.2em;">'.$publishTime.'</p>';
echo '<p style="font-size:1.4em;color:#FF714D"><strong>'.$title.'</strong></p>';
echo '<p style="font-size:1.2em;">'.$description. '</p>';
echo '<p style="margin-top:4%;"><a class="btn btn-lg btn-success" href="https://www.youtube.com/watch?v='.$id.'">VIEW VIDEO</a></p>';
echo '</div>';
echo '<div class="col-md-5 align-left"><iframe width="560" height="315" src="https://www.youtube.com/embed/'.$id.'?rel=0&autoplay=1" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>';
//echo '<div class="col-md-5 align-left"><a target="_blank" href="https://www.youtube.com/watch?v='.$id.'"><img class="img-responsive" alt="'.$title.'" src="'.$mediumThumbnail.'" title="'.$title.'" /></a></div>';
echo '</div>';
}
echo '</div>';
?>
我基本上想获得类似的简单结果,很高兴看到有关如何管理身份验证过程并在 html 页面上显示响应的编码示例,而无需安装任何库。
感谢您的帮助。
这里是 youtube.liveBroadcasts.list 方法的一个基本示例。
<?php
/**
* Sample PHP code for youtube.liveBroadcasts.list
* See instructions for running these code samples locally:
* https://developers.google.com/explorer-help/guides/code_samples#php
*/
if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
throw new Exception(sprintf('Please run "composer require google/apiclient:~2.0" in "%s"', __DIR__));
}
require_once __DIR__ . '/vendor/autoload.php';
$client = new Google_Client();
$client->setApplicationName('API code samples');
$client->setScopes([
'https://www.googleapis.com/auth/youtube.readonly',
]);
// TODO: For this request to work, you must replace
// "YOUR_CLIENT_SECRET_FILE.json" with a pointer to your
// client_secret.json file. For more information, see
// https://cloud.google.com/iam/docs/creating-managing-service-account-keys
$client->setAuthConfig('YOUR_CLIENT_SECRET_FILE.json');
$client->setAccessType('offline');
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open this link in your browser:\n%s\n", $authUrl);
print('Enter verification code: ');
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
// Define service object for making API requests.
$service = new Google_Service_YouTube($client);
$queryParams = [
'id' => 'YOUR_BROADCAST_ID'
];
$response = $service->liveBroadcasts->listLiveBroadcasts('snippet,contentDetails,status', $queryParams);
print_r($response);
我已经设法以不同的方式获得了 scheduledStartTime 值,没有 Oauth2。
基本上,我使用了两个 api 调用: 第一个是
https://youtube.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId=' .$channelId. '&maxResults=' .$maxResults. '&eventType=upcoming&type=video&key=' .$key;
然后对于任何视频 ID,我调用视频端点:
https://youtube.googleapis.com/youtube/v3/videos?part=liveStreamingDetails&id=' .$id. '&key=' .$key;
在这里我可以提取任何视频的 scheduledStartTime 值。