我正在尝试使用Zend_Gdata(Zend Framework 1.12.0)通过API将视频上传到YouTube。我在直接上传工作时没有问题,但基于浏览器的上传总是给我一个400-INVALID TOKEN错误。我很确定我一定错过了一些重要的东西,但小到不会注意到
这涉及到两个文件:
index.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>
和上传完成.php
<?php
print nl2br(print_r($_GET, true));
print nl2br(print_r($_POST, true));
我在Stack Overflow上搜索了这两个网站,并在谷歌上搜索了几个小时,但没有找到任何解决问题的方法,这让我相信我错过了一些非常简单的东西。如有任何帮助,我们将不胜感激。
注意:此代码仅用于测试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/)。生产代码将以更结构化的方式重写,但目前这并不重要。
您的action="<?= $url ?>.?nexturl=<?= urlencode($nextUrl) ?>"
看起来可疑;是不是在你的$url
变量被求值后,里面有一个错误的.
字符,把URL弄乱了?