Twitter视频上传Java [英] Twitter Video Upload Java

查看:118
本文介绍了Twitter视频上传Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Twitter上使用Java上传视频,但由于相同的原因{错误":[{代码":214,消息":错误的请求."}}}导致失败.

Am trying to upload video using Java on twitter and it keeps failing for same reason {"errors":[{"code":214,"message":"Bad request."}]}.

Twitter说,视频需要对base64编码的媒体文件块进行编码.我下面的代码用于上传相同的代码

Twitter says the video needs to base64-encoded chunk of media file. My code below for uploading the same

    String url = "https://upload.twitter.com/1.1/media/upload.json?command=APPEND&media_id="+mediaId+"&segment_index=0";

    CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(this.consumerKey, this.consumerSecret); 
    consumer.setTokenWithSecret(token,tokenSecret);

    HttpPost request = new HttpPost(url);
    request.setHeader("content-type", "multipart/form-data");


    MultipartEntity multiPartEntity = new MultipartEntity () ;

    /*
    multiPartEntity.addPart("command", new StringBody("APPEND")) ;
    multiPartEntity.addPart("media_id", new StringBody(mediaId)) ;
    */

    //FileBody fileBody = new FileBody(file) ;
    byte[] file64 = loadFileAsBytesArray(file);
    ContentBody cd = new InputStreamBody(new ByteArrayInputStream(file64), file.getName());
    multiPartEntity.addPart("media_data",  cd);

    /*
    multiPartEntity.addPart("media_data", cd) ;
    multiPartEntity.addPart("segment_index", new StringBody("0") ) ;
     */
    request.setEntity(multiPartEntity);
    consumer.sign(request);

    DefaultHttpClient httpClient = new DefaultHttpClient(); 
    HttpResponse response = httpClient.execute(request);

    int code = response.getStatusLine().getStatusCode();

    System.err.println("Code "+code);


 public static byte[] loadFileAsBytesArray(File file) throws Exception 
{
    int length = (int) file.length();
    BufferedInputStream reader = new BufferedInputStream(new FileInputStream(file));
    byte[] bytes = new byte[length];
    reader.read(bytes, 0, length);
    reader.close();
    return bytes;
}

我能够获取媒体ID,因此请确保授权(如果可以).上传基本64文件时我丢失了什么吗?

I am able to get the media id, so am sure the authorization if fine. Am I missing something in uploading the base 64 file?

推荐答案

据我所知,一旦将媒体上传到media/upload.json端点,我们就必须再次调用statuss/update_with_media.json端点通过 上面返回的mediaId会将媒体状态也发布到Twitter ...请更正并提出建议...

As far as I unerstand, once we upload media to media/upload.json end point, we have to make another call to statuses/update_with_media.json endpoint by passing mediaId returned above which will post the media to twitter with the status also...Please correct and suggest...

这篇关于Twitter视频上传Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆