Youtube api v3基于浏览器的上传抛出400错误



我正在进行基于youtube浏览器的视频上传。但它抛出了一个错误。

Fatal error: Uncaught exception 'Zend_Gdata_App_HttpException' with message 'Expected response code 200, got 400 <?xml version='1.0' encoding='UTF-8'?><errors><error><domain>yt:validation</domain><code>invalid_value</code><location type='xpath'>media:group/media:category[@scheme='http://gdata.youtube.com/schemas/2007/categories.cat']/text()</location></error></errors>' in /home/xxx/public_html/yyy.com/up/Zend/Gdata/App.php:714 Stack trace: #0 /home/xxx/public_html/yyy.com/up/Zend/Gdata.php(219): Zend_Gdata_App->performHttpRequest('POST', 'http://gdata.yo...', Array, '<atom:entry xml...', 'application/ato...', NULL) #1 /home/xxx/public_html/yyy.com/up/Zend/Gdata/App.php(905): Zend_Gdata->performHttpRequest('POST', 'http://gdata.yo...', Array, '<atom:entry xml...', 'application/ato...') #2 /home/xxx/public_html/yyy.com/up/Zend/Gdata/YouTube.php(704): Zend_Gdata_App->post(Object(Zend_Gdata_YouTube_VideoEntry), 'http://gdata.yo...') #3 /home/xxx/public_html/yyy.com/up/video_upload. in /home/xxx/public_html/yyy.com/up/Zend/Gdata/App.php on line 714

代码:

<?php
// video_upload.php

require_once 'Zend/Loader.php'; // the Zend dir must be in your include_path
Zend_Loader::loadClass('Zend_Gdata_YouTube');
$yt = new Zend_Gdata_YouTube();
// Define the authentication that will be used
Zend_Loader::loadClass('Zend_Gdata_ClientLogin'); 
// Authenticate
$authenticationURL= 'https://www.google.com/accounts/ClientLogin';
$httpClient = 
Zend_Gdata_ClientLogin::getHttpClient(
$username = "example@gmail.com",
$password = "example",
$service = 'youtube',
$client = null,
$source = 'HTML SOURCE CODE SNIPPET',
$loginToken = null,
$loginCaptcha = null,
$authenticationURL);
$applicationId = 'YOUR APPLICATION ID';
$clientId = 'Upload videos to youtube using the youtube API';
$developerKey = 'A...........................0';
$yt = new Zend_Gdata_YouTube($httpClient, $applicationId, $clientId, $developerKey);

// create a new VideoEntry object
$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
$myVideoEntry->setVideoTitle('My');
$myVideoEntry->setVideoDescription('video');
// The category must be a valid YouTube category!
$myVideoEntry->setVideoCategory('news');
// Set keywords. Please note that this must be a comma-separated string
// and that individual keywords cannot contain whitespace
$myVideoEntry->SetVideoTags('news');
$tokenHandlerUrl = 'http://gdata.youtube.com/action/GetUploadToken';
$tokenArray = $yt->getFormUploadToken($myVideoEntry, $tokenHandlerUrl);
$tokenValue = $tokenArray['token'];
$postUrl = $tokenArray['url'];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $postUrl."?nexturl=//example.com/up");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, true);
// same as <input type="file" name="file">
$post = array("file"=>"@".$VideoFile['tmp_name'], "token"=>$tokenValue);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
$response = curl_exec($ch);
$info = curl_getinfo($ch);
echo $info;
?>

形式:

<form action="video_upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="uploadedfile"><br>
<input type="submit" value="Upload File to Server">
</form>

我想直接将视频上传到我的youtube帐户。所以用户可以将视频上传至我的youttube帐户。。

所以我不向$applicationId$clientId添加任何内容。

我认为$applicationId$clientId需要做些什么。

感谢

更新:

正如Wanny Miarelli建议的那样我将news更改为News,现在没有显示错误,但视频也没有上传。

尝试了print_r($info)并得到了这个

Array
(
[url] => http://uploads.gdata.youtube.com/action/FormDataUpload/AIwbFAS1SkX04C09Re5sWeYXb562vc7W5KHcRu0uZcJ7v-A4nX_21zVwIOxdeBtp_s4tj-UcnZxJaN-Zd7nKVUfWwtWxW2YEPg?nexturl=//example.com/up
[content_type] => 
[http_code] => 0
[header_size] => 0
[request_size] => 0
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.024856
[namelookup_time] => 0.018251
[connect_time] => 0.024751
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => -1
[starttransfer_time] => 0
[redirect_time] => 0
[certinfo] => Array
(
)
[redirect_url] => 
)

是的,这似乎是一个类别问题。尝试将"新闻"更改为"新闻">

在那里查找完整的类别列表

https://developers.google.com/youtube/2.0/reference#Region_specific_feeds

这是直接下载到.cat文件

http://gdata.youtube.com/schemas/2007/categories.cat

这是谷歌应该在视频上传(成功或失败)后发送给你的

上传视频文件后,用户将重定向到您表单中指定的下一个URL。如果上传成功,YouTube将id和状态参数附加到URL,如下所示示例:

http://www.example.com/youtube_uploads?status=200&id=JPF-DXF7hzc如果上传不成功,YouTube会附加状态和代码URL的参数,以帮助您诊断故障原因。参考指南提供了有关这些参数的更多信息。

好的,我必须建立一个zend框架istance,这段代码适用于我的

*此代码仅用于解释-不安全且不优化*

用户想用一步表单上传视频,这是的解决方案

<?php
//single file script, need to know if we are in a POST or GET request, this needs to be changed, dividing the script into a separate file 
if(isset($_FILES['file']['name'])){

Zend_Loader::loadClass('Zend_Gdata_YouTube');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
$authenticationURL= 'https://www.google.com/accounts/ClientLogin';
$httpClient = Zend_Gdata_ClientLogin::getHttpClient(
$username = '',
$password = '',
$service = 'youtube',
$client = null,
$source = 'TestApp', // a short string identifying your application
$loginToken = null,
$loginCaptcha = null,
$authenticationURL);

$applicationId = 'TestAPP';
$developerKey = "---";
$clientId = "";
// Note that this example creates an unversioned service object.
// You do not need to specify a version number to upload content
// since the upload behavior is the same for all API versions.
$yt = new Zend_Gdata_YouTube($httpClient, $applicationId, $clientId, $developerKey);
// create a new VideoEntry object
$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
$myVideoEntry->setVideoTitle($_POST['Title']);
$myVideoEntry->setVideoDescription($_POST['Description']);
// The category must be a valid YouTube category!
$myVideoEntry->setVideoCategory('News');
// Set keywords. Please note that this must be a comma-separated string
// and that individual keywords cannot contain whitespace
$myVideoEntry->SetVideoTags('News');
$tokenHandlerUrl = 'http://gdata.youtube.com/action/GetUploadToken';
$tokenArray = $yt->getFormUploadToken($myVideoEntry, $tokenHandlerUrl);
$tokenValue = $tokenArray['token'];
$postUrl = $tokenArray['url'];
// place to redirect user after upload
$nextUrl = 'http://localweb/';
$target_url = 'http://127.0.0.1/accept.php';
//This needs to be the full path to the file you want to send.
$file_name_with_full_path = realpath('C:video.flv');
/* curl will accept an array here too.
* Many examples I found showed a url-encoded string instead.
* Take note that the 'key' in the array will be the key that                    shows up in the
* $_FILES array of the accept script. and the at sign '@' is required before the
* file name.
*/
// Post the video to Youtube Server
$post = array('token' => $tokenValue,'file_contents'=>'@'. $_FILES["file"]["tmp_name"]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $postUrl . '?nexturl=' . $nextUrl);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;

}else{
$self = $_SERVER['PHP_SELF'];
$form = '<form action="' . $self  . '"'.
' method="post" enctype="multipart/form-data">'. 
'<input name="file" id="file" type="file"/>'. 
'<input name="token" id="token" type="hidden" value=""/>'.
'<input name="Description" id="Description" />'.
'<input name="Title" id="Title" />'.
'<input value="Upload Video File" type="submit" />'. 
'</form>';
echo $form;
}

相关内容

  • 没有找到相关文章

最新更新