问题
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?
回答1:
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...
来源:https://stackoverflow.com/questions/38422875/twitter-video-upload-java