我尝试使用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>
和 uploadDone.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/)。生产代码将以更有条理的方式重写,但目前并不重要。
答案 0 :(得分:1)
您的action="<?= $url ?>.?nexturl=<?= urlencode($nextUrl) ?>"
看起来很可疑;在您的.
变量被评估之后,是否存在错误的$url
字符,弄乱了网址?