2011-10-31 51 views
0

我正嘗試使用圖API將視頻上傳到用戶牆。結果始終是「{」error「:{」message「:」(#352)不支持視頻文件格式「,」type「:」OAuthException「}}」的錯誤響應。我嘗試了幾種不同的視頻類型,這些視頻類型都是基於此列表支持的,http://developers.facebook.com/docs/reference/api/video/。根據我對我找到的文檔的理解,所有需要完成的工作是通過POST將多部分表單數據請求發送到「https://graph-video.facebook.com/me/videos」。順便說一句,我已經能夠使用類似的技術發佈照片。我使用的代碼如下。它基於的例子是http://developers.facebook.com/blog/post/493/。我已經能夠使用facebook上傳機制上傳不同的視頻,所以我知道Facebook的視頻是可以的。訪問令牌是有效的,因爲我已經使用它通過Graph API發佈照片。發佈視頻時始終不支持視頻格式

任何建議,我缺少的是歡迎!

下面是我使用的Java代碼:

File video = new File(pathtovideofile); 
DataInputStream dis = new DataInputStream(new FileInputStream(video)); 
byte[] bytes = new byte[(int)video.length()];   
dis.read(bytes, 0, (int)video.length()); 

// set up the http client, the http method, and the multipart entity 
DefaultHttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("https://graph-video.facebook.com/me/videos"); 
MultipartEntity mpEntity = new MultipartEntity(); 

ContentBody cbVideo  = new ByteArrayBody(bytes, "video/mp4", "Video Label"); 
ContentBody cbMessage  = new StringBody("New Video"); 
ContentBody cbTitle  = new StringBody("Video Title"); 
ContentBody cbAccessToken = new StringBody(accessTokenStr1); 

mpEntity.addPart("access_token", cbAccessToken); 
mpEntity.addPart("file",   cbVideo  ); 
mpEntity.addPart("description", cbMessage ); 
mpEntity.addPart("title",  cbTitle ); 

// put the multipart entity into the request 
httppost.setEntity(mpEntity); 
// send the request 
HttpResponse response = httpclient.execute(httppost); 

// get the response entity 
HttpEntity resEntity = response.getEntity(); 
// read the stream and print out the results 
InputStream instream = resEntity.getContent(); 
BufferedReader reader = new BufferedReader(new InputStreamReader(instream)); 
String line; 
StringBuilder responsestr = new StringBuilder(); 
while ((line = reader.readLine()) != null) {    
    responsestr.append(line); 
} 
System.out.println(responsestr.toString()); 

回答

2

在PHP中,這爲我工作。首先將文件上傳到服務器,然後使用Graph API嘗試API調用。

$fbvideo_upload=move_uploaded_file($_FILES['attach_video']['tmp_name'],$fbvideo_path); 
chmod($fbvideo_path,0777);     
if($fbvideo_upload) 
{ 
    $args = array('message' => $status, "access_token" =>$accesstoken,"file" 
      => '@'.$fbvideo_path, "title"=>$video_title, "description"=>$video_desc); 
    $post_url = "https://graph-video.facebook.com/me/videos?access_token=".$accesstoken; 
    $ch = curl_init();     
    curl_setopt($ch, CURLOPT_URL, $post_url); 
    curl_setopt($ch, CURLOPT_HEADER, false); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $args); 
    $data = curl_exec($ch);      
    $data=json_decode($data,true);      
    if(file_exists($fbvideo_path)) 
    {       
     @unlink($fbvideo_path);       
    } 
}