使用YouTube V3 API回显视频ID

时间:2018-12-24 18:17:54

标签: php linux youtube-api

10月份,Google决定对他们的直播服务进行无记录的更改,这让我困惑了几个月。最终找到问题后,我开始研究YouTube V3 API的解决方案,但是我开始认为这是不可能的,因此,我非常感谢我对要实现的目标的一些反馈。

当前,在YouTube上进行直播时,您会获得3种隐私选项;

  • 公开-您可以通过频道ID进行嵌入,这表示频道是永久的。
  • 不公开-您只能通过视频ID进行嵌入,而当您开始新视频流时,视频ID会发生变化。
  • 私有

在寻找一种自动获取未公开视频变量(视频ID)的方法时,我遇到了YouTube(https://github.com/youtube/api-samples)提供的以下代码;

<?php

/**
* Library Requirements
*
* 1. Install composer (https://getcomposer.org)
* 2. On the command line, change to this directory (api-samples/php)
* 3. Require the google/apiclient library
*    $ composer require google/apiclient:~2.0
*/
if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
throw new \Exception('please run "composer require google/apiclient:~2.0" in "' . __DIR__ .'"');
}

require_once __DIR__ . '/vendor/autoload.php';
session_start();

/*
* You can acquire an OAuth 2.0 client ID and client secret from the
* {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}>
* For more information about using OAuth 2.0 to access Google APIs, please see:
* <https://developers.google.com/youtube/v3/guides/authentication>
* Please ensure that you have enabled the YouTube Data API for your project.
*/
$OAUTH2_CLIENT_ID = 'REPLACE_ME';
$OAUTH2_CLIENT_SECRET = 'REPLACE_ME';

$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);

// Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube($client);

// Check if an auth token exists for the required scopes
$tokenSessionKey = 'token-' . $client->prepareScopes();
if (isset($_GET['code'])) {
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
die('The session state did not match.');
}

$client->authenticate($_GET['code']);
$_SESSION[$tokenSessionKey] = $client->getAccessToken();
header('Location: ' . $redirect);
}

if (isset($_SESSION[$tokenSessionKey])) {
$client->setAccessToken($_SESSION[$tokenSessionKey]);
}

// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
try {
// Call the channels.list method to retrieve information about the
// currently authenticated user's channel.
$channelsResponse = $youtube->channels->listChannels('contentDetails', array(
'mine' => 'true',
));

$htmlBody = '';
foreach ($channelsResponse['items'] as $channel) {
// Extract the unique playlist ID that identifies the list of videos
// uploaded to the channel, and then call the playlistItems.list method
// to retrieve that list.
$uploadsListId = $channel['contentDetails']['relatedPlaylists']['uploads'];

$playlistItemsResponse = $youtube->playlistItems->listPlaylistItems('snippet', array(
'playlistId' => $uploadsListId,
'maxResults' => 50
));

$htmlBody .= "<h3>Videos in list $uploadsListId</h3><ul>";
foreach ($playlistItemsResponse['items'] as $playlistItem) {
$htmlBody .= sprintf('<li>%s (%s)</li>', $playlistItem['snippet']['title'],
$playlistItem['snippet']['resourceId']['videoId']);
}
$htmlBody .= '</ul>';
}
} 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()));
}

$_SESSION[$tokenSessionKey] = $client->getAccessToken();
} elseif ($OAUTH2_CLIENT_ID == 'REPLACE_ME') {
$htmlBody = <<<END
<h3>Client Credentials Required</h3>
<p>
You need to set <code>\$OAUTH2_CLIENT_ID</code> and
<code>\$OAUTH2_CLIENT_ID</code> before proceeding.
<p>
END;
} else {
$state = mt_rand();
$client->setState($state);
$_SESSION['state'] = $state;

$authUrl = $client->createAuthUrl();
$htmlBody = <<<END
<h3>Authorization Required</h3>
<p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
END;
}
?>

<!doctype html>
<html>
<head>
<title>My Uploads</title>
</head>
<body>
<?=$htmlBody?>
</body>
</html>

上面的例子很棒,它可以按我的需要工作。当我通过网络浏览器导航到/my_upload.php(以上来源)时,我能够通过OAuth登录到我的YouTube帐户并打印与我上传的视频一致的视频ID列表。有趣的是,当我将“ maxResults”选项更改为1时,它将列出最新的视频ID,如果我当前正在直播,则该视频ID将是最新的。据我了解,一旦您通过了身份验证,就不需要重新进行身份验证,但是我在后端服务器上进行身份验证时遇到了问题。

我想要建立的系统涉及使用cron,例如,让bash脚本执行以下操作;

#!/bin/bash
ffmpeg -whatever -launch -parameters -I -need ### Launch the livestream
timeout 300 seconds ### Pause for 5 Minutes to allow YouTube to go live
curl/php path/to/my_uploads.php ### Run the php script to obtain video ID

每当计划了cron时,它将启动实时流,然后运行my_uploads.php脚本来获取视频ID,该视频ID将保存在.txt文档中,准备回显到Web播放器中。

那是什么问题?

我已经通过在浏览器中转到/my_uploads.php文件成功地进行了身份验证,但是它不对后端服务器进行身份验证,我正在使用VNC连接到后端服务器进行身份验证。我认为这就像做类似的事情一样简单;

curl http://backendserverip/my_uploads.php -> Follow auth steps

但是当然不能,Google不允许使用IP,因此出于测试目的,我编辑了/ etc / hosts文件以反映以下内容;

myserverip localhost.mydomain.com

根据Google接受的功能,哪个功能正常。太好了,现在应该可以运作了。当我执行以下操作时;

curl http://localhost.mydomain.com/my_uploads.php (ignore index.php)

a busy cat

访问终端上显示的链接;

a busy cat

我收到一条错误消息,指出“会话状态不匹配”,我猜这是因为身份验证URL是在终端内生成的,并且当我在浏览器中输入链接时,标头和其他内容有所不同,因此出于安全性考虑不允许我进行身份验证。

当然,必须有一种方法可以使我的服务器进行身份验证才能访问API,我肯定做错了什么,因为对于这种情况为什么存在,这绝对没有任何意义。如果这仅是基于Web的用法,那么这个世界上谁可以访问第三方站点,请通过OAuth登录以回显其视频数据。他们为什么不直接使用YouTube?

让我感到困惑的是,我已经使用VNC连接到服务器。打开firefox,导航至my_uploads.php文件,并对其进行固定,并可以正常运行;

a busy cat

但是,当通过终端/ putty执行命令时;

curl http://localhost.mydomain.com/my_uploads.php (ignore index.php)

我被迫重新进行身份验证,但是据我所知,我无法通过终端进行身份验证。

我是白痴吗?这甚至是不可能的还是我在这里做的完全错误的事情,我花了过去的两天时间试图弄清楚这一点。我在途中学到了很多东西,但是我绝对迷路了,在这里我确实需要一些帮助,谢谢您的时间,对于冗长的帖子深表歉意。

0 个答案:

没有答案