【发布时间】:2012-05-21 11:16:08
【问题描述】:
所以我设法构建了 oauth 2.0 youtube 视频上传,但每次我上传视频时,我都会收到 HTTP 400 错误,请求无效。
但最奇怪的是,视频上传到 youtube 时出现:失败(上传中止)。
我没有使用任何框架,因为谷歌还没有任何 oauth 2.0,所以我自己构建了所有代码。
而且我确实设法发送了 cmets 和其他东西......唯一的问题是视频上传本身。
我的代码:
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 Bad Request Server: HTTP Upload Server Built on May 7 2012 18:16:42 (1336439802) Content-Type: text/html; Charset = UTF-8 X-GUPLOODER-UNENB2UQ7CHCF6RS4BCAMU18CHAF3GNKJQSF6U_DK2QB4WR9GHAOTL_-IUEJITGEAD-GH-1FPJCCKE1Z68TAXOOPS2VYIGMCWW69A日期:THU,2012年5月11日11:55:24 GMT Pragma:No-Cache到期:FRI,20190 00:00 GMT缓存-控制:无缓存,无存储,必须重新验证内容长度:15 连接:关闭
无效请求
谢谢大家!
【问题讨论】: