【发布时间】:2021-05-16 02:16:22
【问题描述】:
我需要从我的 Youtube 频道获取即将进行的直播列表并将其展示给我的网站访问者。
问题是通过简单的 API 密钥 http 请求 ('https://youtube.googleapis.com/youtube/v3/') 我无法访问 scheduleStartTime 值。
这样做的唯一方法是使用 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 页面上显示响应的编码示例,而无需安装任何库,那就太好了。
感谢您的帮助。
【问题讨论】:
-
我希望这是私人用户数据,您需要作为频道所有者进行身份验证才能看到类似的内容。你有没有尝试过?使用 Oauth2 我们可以看到你的代码吗?
-
我想知道从哪里开始使用 Oauth2 代码。我的 ID 客户端 OAuth 2.0 已经从 console.cloud.google.com 配置我已经在测试控制台 developers.google.com/youtube/v3/live/docs/liveBroadcasts 上测试了 api 调用,它返回了我需要的信息... example scheduledStartTime": "2021-02-16T15: 00:00Z”,我只是不明白如何以最简单的方式实现身份验证。
标签: php oauth-2.0 youtube-api google-oauth youtube-data-api