2011-06-23 85 views
4

當我嘗試使用youtube API上傳視頻時,出現「無效請求」錯誤。Youtube API(無效請求)有什麼問題?

但是!

當我上傳任何「圖像」而不是「視頻」時,它會返回狀態「200」和視頻ID。

爲什麼我不能上傳視頻?

$eq = 'accountType=HOSTED_OR_GOOGLE&Email='.$YOUTUBE_EMAIL.'&Passwd='.$YOUTUBE_PASS.'&service=youtube&source='.$API_NAME; 
if ($fp = fsockopen ("ssl://www.google.com", 443, $errno, $errstr, 20)) 
{ 
    $request ="POST /youtube/accounts/ClientLogin HTTP/1.0\r\n"; 
    $request.="Host: www.google.com\r\n"; 
    $request.="Content-Type:application/x-www-form-urlencoded\r\n"; 
    $request.="Content-Length: ".strlen($eq)."\r\n"; 
    $request.="\r\n\r\n"; 
    $request.=$eq; 
    fwrite($fp,$request,strlen($request)); 

    while (!feof($fp)) 
    $response.=fread($fp,8192); 
    //fclose($fp); 
} 

preg_match("!(.*?)Auth=(.*?)\n!si",$response,$ok); 
$AUTH_TOKEN = $ok[2]; 

$data = "<?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'>test</media:title> 
    <media:description type='plain'>test</media:description> 
    <media:category scheme='http://gdata.youtube.com/schema /2007/categories.cat'>People</media:category> 
    <media:keywords>toast, wedding</media:keywords> 
    </media:group> 
</entry> "; 

if ($fp = fsockopen ("gdata.youtube.com", 80, $errno, $errstr, 20)) 
{ 
    $request ="POST /action/GetUploadToken HTTP/1.1\r\n"; 
    $request.="Host: gdata.youtube.com\r\n"; 
    $request.="Content-Type: application/atom+xml; charset=UTF-8\r\n"; 
    $request.="Content-Length: ".strlen($data)."\r\n"; 
    $request .="Authorization: GoogleLogin auth=".$AUTH_TOKEN."\r\n"; 
    $request.="X-GData-Client: ".$API_NAME." \r\n"; 
    $request.="X-GData-Key: key=".$API_KEY." \r\n"; 
    $request.="\r\n"; 
    $request.=$data."\r\n"; 

    socket_set_timeout($fp, 10); 
    fputs($fp,$request,strlen($request)); 
    $response = fread($fp,3280); 
    fclose($fp); 
} 


preg_match('|<url>(.*)</url>|Uis', $response, $url); 
preg_match('|<token>(.*)</token>|Uis', $response, $token); 

print " 
    <form action='".$url[1]."?nexturl=http%3A%2F%2Fwww.google.com' method='post' enctype='multipart/form-data'> 
     <input type='file' name='file'> 
     <input type='hidden' name='token' value='".$token[1]."'> 
     <input type='submit' value='go'> 
    </form> "; 
+1

你爲什麼不使用YouTube的PHP客戶端庫來做到這一點? –

+0

我使用ZEND庫,但此變體不起作用。 我得到這個錯誤: >致命錯誤:未捕獲的異常 > 'Zend_Gdata_App_HttpException' 與 >消息 '預期響應碼200, >得到400 編碼='UTF- 8 '> YT:驗證required<位置 >類型=' 的xpath '>媒體:基/媒體:標題/文本()' >在 > /家庭/關機/數據/萬維網/online-gid.ru/YouTubeAPI/Zend/Gdata/App.php:700 >堆棧軌跡: #0 > /home/switchoff/data/www/online-gid.ru/YouTubeAPI/Zend/Gdata.php(219): – Nick

+0

> Zend_Gdata_App-> performHttpRequest('POST','http://gdata.yo。 ..',Array, >''application/ato ...',NULL)#1 > /home/switchoff/data/www/online-gid.ru /YouTubeAPI/Zend/Gdata/App.php(886): > Zend_Gdata-> performHttpRequest('POST', >'http://gdata.yo ...',Array, >''application/ato ...')#2 > /home/switchoff/data/www/online-gid.ru/YouTubeAPI/Zend/Gdata/YouTube.php(704): > Zend_Gdata_App-> post(Object(Zend_Gdata_YouTube_VideoEntry), >'http://gdata.yo ...')#3 >/home /switchoff/data/www/online-gid.ru/YouTubeAPI/test.php(56): – Nick

回答

2

哈哈哈!差不多一年之後,我想我找到了解決辦法。

你行 「/模式」 之後有一個空格和 「/ 2007年」 前

<media:category scheme='http://gdata.youtube.com/schema /2007/categories.cat'>People</media:category> 

應該

<media:category scheme='http://gdata.youtube.com/schema/2007/categories.cat'>People</media:category> 

看到下第二個代碼塊:https://developers.google.com/youtube/2.0/developers_guide_protocol_direct_uploading#Sending_a_Direct_Upload_API_Request