Error code 214 while uploading video

java
api

#1

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

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;
}

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?


#2

Hello @rohitgu,

I am trying to achieve the same behaviour to upload video to twitter…could you please share he code that you are using to uploa the video and also please let me know how you are handling authorisation here…