【发布时间】:2011-12-19 00:46:28
【问题描述】:
我正在尝试使用图形 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/ 的 PHP 示例。我已经能够使用 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());
【问题讨论】:
标签: java video facebook-graph-api upload