在没有最终用户身份验证的情况下创建播放列表 - YouTube API v3

时间:2013-01-27 00:09:14

标签: php youtube-api google-api-php-client

我正在尝试找出一种方法来创建播放列表,上传视频等,而无需自己进行用户身份验证,而是让服务器使用我的YouTube帐户为他们执行此操作。

我开始只是添加mine参数的频道列表,因为这需要身份验证。

以下是我使用的代码,结果可以在http://www.daysofthedead.net/testing/playlist.php

看到

您可以在底部看到代码有效,因为我可以从不需要身份验证的搜索中提取结果。但是频道列表是空的,这需要验证。

<?php
require_once 'GoogleClient/Google_Client.php';
require_once 'GoogleClient/contrib/Google_YoutubeService.php';

// Set your client id, service account name, and the path to your private key.
// For more information about obtaining these keys, visit:
// https://developers.google.com/console/help/#service_accounts
const CLIENT_ID = 'xxxxxxxxxx.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = 'xxxxxxxxxx8@developer.gserviceaccount.com';

// Make sure you keep your key.p12 file in a secure location, and isn't
// readable by others.
const KEY_FILE = 'xxxxxxxxxx-privatekey.p12';

$client = new Google_Client();
$client->setApplicationName("Google YouTube Sample");

// Set your cached access token. Remember to replace $_SESSION with a
// real database or memcached.
session_start();
if (isset($_SESSION['token'])) {
 $client->setAccessToken($_SESSION['token']);
}

// Load the key in PKCS 12 format (you need to download this from the
// Google API Console when the service account was created.
$key = file_get_contents(KEY_FILE);
$client->setAssertionCredentials(new Google_AssertionCredentials(
    SERVICE_ACCOUNT_NAME,
    array('https://www.googleapis.com/auth/youtube'),
    $key)
);

$client->setClientId(CLIENT_ID);
$service = new Google_YouTubeService($client);

# User to search Youtube for.
$search_user = "BruceLeeOfficialPage";

# listSearch optional parameters
$search_optParams = array('part' => 'snippet', 'q' => $search_user, 'maxResults' => 2, 'type' => 'video');
# Search YouTube
$search = $service->search->listSearch($search_optParams);

# listChannels optional parameters.
$channel_optParams = array('maxResults' => 50, 'mine' => 'true');
# Get YouTube user's channel information
$channel = $service->channels->listChannels('snippet', $channel_optParams);

// We're not done yet. Remember to update the cached access token.
// Remember to replace $_SESSION with a real database or memcached.
if ($client->getAccessToken()) {
  $_SESSION['token'] = $client->getAccessToken();
}
?>
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <link rel='stylesheet' href='style.css' />
</head>
<body>
<header><h1>YouTube API v3 Sample App</h1></header>
<div class="box">

<?php if(isset($search)): ?>
<div class="search"><H1>Search:</H1>
<pre><?php print_r($search); ?></pre></div>
<?php endif ?>

<?php if(isset($channel)): ?>
<div class="channel"><H1>Channel:</H1>
<pre><?php print_r($channel); ?></pre></div>
<?php endif ?>

</div>
</body>
</html>

任何人都可以帮我解决这个问题吗?有什么工作吗?代码示例将非常感激。谢谢。

1 个答案:

答案 0 :(得分:2)

您尝试将OAuth 2用于服务帐户流程,YouTube API不支持此流程。支持的流列表记录在https://developers.google.com/youtube/v3/guides/authentication

通常,您需要使用Web浏览器浏览一次支持流程,从那时起,您可以在没有用户干预的情况下发出请求。