2012-05-13 190 views
2

因此,我設法建立了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:接近

無效請求

感謝名單大家!

回答

1

我注意到一些事情:硬編碼POST和Host頭是不好的形式,因爲curl會爲你自動照顧它們。我懷疑部分問題是在$ videoData和最後一個邊界標記之間插入了回車符/換行符。這將被視爲視頻文件的一部分。您所需要的只是換行符作爲行分隔符。也許回車渲染視頻文件無效?

也許curl_setopt($ ch,CURLOPT_VERBOSE,true)會提供一些照明。

這對我的作品(在Linux主機上):

/* 
    ** https://developers.google.com/youtube/2.0/developers_guide_protocol_direct_uploading 
    */ 
    private function send_to_youtube($video_file, $video_info) { 

     // Refresh access token 
     log_msg("Obtaining access token"); 
     $response = http_post($this->config['token_url'], array(
      'client_id'  => $this->config['client_id'], 
      'client_secret' => $this->config['client_secret'], 
      'refresh_token' => $video_info->refresh_key, 
      'grant_type' => $this->config['grant_type'] 
     )); 
     if ($response['http_code'] != 200) 
      throw new Exception("Unable to obtain access token. ".print_r($response, true)); 
     $authorization = json_decode($response['contents'], true); 

     // Build multi-part upload request 
     // api xml and then video file contents 
     $boundary = uniqid(); 
     $location = '';   
     if ($video_info->latitude && $video_info->longitude) 
      $location = ' 
    <georss:where> 
    <gml:Point> 
     <gml:pos>'. $video_info->latitude .' '. $video_info->longitude .'</gml:pos> 
    </gml:Point> 
    </georss:where>'; 

     $content = '--'.$boundary.' 
Content-Type: application/atom+xml; charset=UTF-8 

<?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" 
    xmlns:georss="http://www.georss.org/georss" 
    xmlns:gml="http://www.opengis.net/gml"> 
    <media:group> 
    <media:title type="plain">'. $video_info->title .'</media:title> 
    <media:description type="plain"> 
     '. $video_info->description .' 
    </media:description> 
    <media:category 
     scheme="http://gdata.youtube.com/schemas/2007/categories.cat"> 
     '. $video_info->category .' 
    </media:category> 
    <media:keywords>'. implode(', ', $video_info->tags) .'</media:keywords> 
    </media:group> 
'. $location .' 
</entry> 
--'.$boundary.' 
Content-Type: '. $video_info->type .' 
Content-Transfer-Encoding: binary 

'.file_get_contents($video_file).' 
--'.$boundary.'--'; 

     $headers = array(
      'Authorization: '.$authorization['token_type'].' '.$authorization['access_token'], 
      'GData-Version: 2', 
      'X-GData-Key: key='.$this->config['dev_key'], 
      'Slug: '.$video_info->filename, 
      'Content-Type: multipart/related; boundary="'.$boundary.'"', 
      'Content-Length: '.strlen($content), 
      'Connection: close' 
     ); 

     // Upload video 
     log_msg("Sending video '{$video_info->title}', {$video_info->url}"); 
     $ch = curl_init($this->config['upload_url']); 
     curl_setopt($ch, CURLOPT_POST, true); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $content); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
     curl_setopt($ch, CURLOPT_HEADER, false); 
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); 
     $response = curl_exec($ch); 
     $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
     curl_close($ch); 

     if (!$http_code == 201) {   // Something other than 'New Entry' 
      log_msg("Upload Failed: ".print_r($response, true)); 
      return new SimpleXMLElement(); 
     } 

     $entry = new SimpleXMLElement($response); 
     $yt_link = $entry->link[0]->attributes()->href; 
     log_msg("Upload Complete: ".$yt_link); 

     return $entry; 
    } 
+0

現在即時得到這個問題(與您的代碼): 畸形多交.....這是什麼意思? – Avihay

+0

我假設錯誤是因爲$ content變量中的文本未被正確組裝。我使用的是Linux主機,如果您使用的是Windows主機,則行結束符不同並可能導致問題。你確認file_get_contents()可以讀取$ video_file指向的文件嗎?在函數聲明上面的註釋中有一個鏈接,它有一個示例,說明完成的$內容應該是什麼樣的,以及所需的頭文件。也許仔細比較它和你所擁有的將會揭示這個問題。 – Carl

+0

carl,即時通訊使用debian主機,所以操作系統不是一個問題:)我確認即時通訊獲取文件的內容,一切都很順利,就像你組裝的函數一樣....這完全是奇怪的。 .. – Avihay