我正在尝试使用 Zend_Gdata(Zend Framework 1.12.0)通过 API 将视频上传到 YouTube。我直接上传工作没有问题,但基于浏览器的上传总是给我一个 400 - INVALID TOKEN 错误。我很确定我一定错过了一些重要但小到不会注意到的东西。
这涉及到两个文件:
索引.php
<?php
$youTubeAPIKey = '<API_Key>';
$username = '<user>';
$password = '<pass>';
set_include_path(get_include_path().PATH_SEPARATOR.__DIR__."/vendor");
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
try
{
$authenticationURL= 'https://www.google.com/accounts/ClientLogin';
$httpClient = Zend_Gdata_ClientLogin::getHttpClient(
$username,
$password,
$service = 'youtube',
$client = null,
$source = 'BrowserUploaderTest', // a short string identifying your application
$loginToken = null,
$loginCaptcha = null,
$authenticationURL);
$yt = new Zend_Gdata_YouTube($httpClient, "browser upload test", "Test version 0.1", $youTubeAPIKey);
$videoEntry = new Zend_Gdata_YouTube_VideoEntry();
$videoEntry->setVideoTitle("Test movie");
$videoEntry->setVideoDescription("This is a test movie");
$videoEntry->setVideoPrivate();
// @todo This must be a valid YouTube category, how to get a list of valid categories?
$videoEntry->setVideoCategory('Autos');
$videoEntry->setVideoTags('cars, funny');
// Get an upload token
$tokenHandlerUrl = 'http://gdata.youtube.com/action/GetUploadToken';
$tokenArray = $yt->getFormUploadToken($videoEntry, $tokenHandlerUrl);
$token = $tokenArray['token'];
$url = $tokenArray['url'];
// print "Token value: {$tokenArray['token']}\n url: {$tokenArray['url']}\n";
$nextUrl = "http://" . $_SERVER['HTTP_HOST'] . "/uploadDone.php";
}
catch (Zend_Gdata_App_HttpException $httpException)
{
echo $httpException->getRawResponseBody();
}
catch (Zend_Gdata_App_Exception $e) {
echo $e->getMessage();
}
catch (Exception $e)
{
print $e->getTraceAsString();
}
?><!DOCTYPE html>
<html>
<head>
<title>Testing Youtube upload</title>
</head>
<body>
<table>
<tr>
<td>
Url:
</td>
<td>
<?= $url ?>
</td>
</tr>
<tr>
<td>
Token:
</td>
<td>
<?= $token ?>
</td>
</tr>
</table>
<form action="<?= $url ?>.?nexturl=<?= urlencode($nextUrl) ?>" enctype="multipart/form-data" method="post">
<input name="token" type="hidden" value="<?= $token ?>" />
<input name="file" type="file" />
<input type="submit" value="Upload file" />
</form>
</body>
</html>
并上传Done.php
<?php
print nl2br(print_r($_GET, true));
print nl2br(print_r($_POST, true));
我已经在 Stack Overflow 上进行了搜索,并在 Google 上搜索了几个小时,但没有找到任何可以解决问题的方法,这让我相信我错过了一些非常简单的东西。任何帮助,将不胜感激。
注意: 此代码仅用于测试 API 的使用,主要取自 Google 的开发人员指南 (https://developers.google.com/youtube/2.0/developers_guide_php#Browser_based_Upload),并在 Yii 框架文档 ( http://www.yiiframework.com/wiki/375/youtube-api-v2-0-browser-based-uploading/)。生产代码将以更结构化的方式重写,但目前这并不重要。