因此,我设法建立了oauth 2.0 youtube视频上传,但每次我上传视频时,我都会收到HTTP 400错误,并显示无效请求。Youtube Oauth 2.0 API视频上传失败
但最奇怪的是,视频正在上传到YouTube,同时有:失败(上传中止)。
即时通讯不使用任何框架,导致谷歌没有任何oauth 2.0,所以我建立了我自己的所有代码。
而且我确实设法发送评论和东西....唯一的问题是视频上传本身。
我的代码:
public function uploadVideo($video, $title, $description, $category, $keywords) {
$url = 'http://uploads.gdata.youtube.com/feeds/api/users/FacebookDevelopersIL/uploads';
$boundary = uniqid();
$accessToken = $this->refreshAccessToken("13", "11313", 'REFRESHTOKEN');
$xmlString = "<?xml version='1.0'?><entry xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:yt='http://gdata.youtube.com/schemas/2007'><media:group><media:title type='plain'>".$title."</media:title><media:description type='plain'>".$description."</media:description> <media:category scheme='http://gdata.youtube.com/schemas/2007/categories.cat'>".$category."</media:category><media:keywords>".$keywords."</media:keywords></media:group></entry>";
$videoData = file_get_contents($video);
$headers = array(
'POST /feeds/api/users/FacebookDevelopersIL/uploads HTTP/1.1',
'Host: uploads.gdata.youtube.com',
'Authorization: Bearer '.$accessToken,
'GData-Version: 2',
'X-GData-Key: key='.YOUTUBE_SRM_DEVELOPER_KEY,
'Slug: IMG_0047.mp4',
'Content-Type: multipart/related; boundary='.$boundary,
'Content-Length:'.strlen($videoData),
'Connection: close'
);
$postData = "--".$boundary . "\r\n"
."Content-Type: application/atom+xml; charset=UTF-8\r\n\r\n"
.$xmlString . "\r\n"
."--".$boundary . "\r\n"
."Content-Type: video/mp4\r\n"
."Content-Transfer-Encoding: binary\r\n\r\n"
.$videoData . "\r\n"
."--".$boundary . "--";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
$response = curl_exec($ch);
curl_close($ch);
Trace::dump($response); }
错误即时得到:HTTP/1.1 400错误的请求服务器:HTTP上传服务器构建于2012年5月7日18时16分42秒(1336439802)的Content-Type:text/html的; charset = UTF-8 X-GUploader-UploadID:AEnB2Uq7cHcf6rS4bcamu18ChAF3gnKJqsF6U_dk2qB4WR9GhAoTL_-iUejitgead-Gh-1fpJcke1z68TAxoopS2vYiGmCW69A Date:Thu,10 May 2012 11:55:24 GMT Pragma:no-cache过期时间:星期五,01 Jan 1990 00:00:00 GMT Cache - 控制:无缓存,无店铺,必重新验证的Content-Length:15:接近
无效请求
感谢名单大家!
现在即时得到这个问题(与您的代码): 畸形多交.....这是什么意思? – Avihay
我假设错误是因为$ content变量中的文本未被正确组装。我使用的是Linux主机,如果您使用的是Windows主机,则行结束符不同并可能导致问题。你确认file_get_contents()可以读取$ video_file指向的文件吗?在函数声明上面的注释中有一个链接,它有一个示例,说明完成的$内容应该是什么样的,以及所需的头文件。也许仔细比较它和你所拥有的将会揭示这个问题。 – Carl
carl,即时通讯使用debian主机,所以操作系统不是一个问题:)我确认即时通讯获取文件的内容,一切都很顺利,就像你组装的函数一样....这完全是奇怪的。 .. – Avihay