Twitter RestAPI 459 Authorization Required

restapi
oauth
media-upload
java

#1

I want to know how to update my status with upload media on twitter using java. Currently my code is able to post the text status but when the same is being used with media upload, am receiving response

459Authorization Required `{"errors":[{"code":32,"message":"Could not authenticate you."}]}`

code :

File file = new File("/tmp/abc.png");
	OAuthConsumer oAuthConsumer = new CommonsHttpOAuthConsumer(consumerKey,consumerSecret);
	oAuthConsumer.setTokenWithSecret(accessToken, accessTokenSecret);
	HttpPost httpPost = new HttpPost("https://api.twitter.com/1.1/statuses/update.json?media_data="+Base64.encodeBase64(FileUtils.readFileToByteArray(file)));
	oAuthConsumer.sign(httpPost);
	CloseableHttpClient httpClient = HttpClientBuilder.create().build();
	HttpResponse httpResponse = httpClient.execute(httpPost);
	int statusCode = httpResponse.getStatusLine().getStatusCode();
	System.out.println(statusCode + ':'+ httpResponse.getStatusLine().getReasonPhrase());
	System.out.println(IOUtils.toString(httpResponse.getEntity().getContent()));

I’ve googled for my solution but found apis like Twitter4j, spring social integration etc but my application does not need them at this stage.

Any help / tutorial that shows Post Status With media(image) on JAVA ?


#2

I don’t have the answer in Java right now, but we have a Python large video upload sample which demonstrates the use of our media upload endpoint.

You cannot attach media blobs directly on the statuses/update endpoint in the way you are doing; you need to post media objects to upload.twitter.com, get the resulting ID, and attach the ID to the status.