Twitter RestAPI 459 Authorization Required



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(""+Base64.encodeBase64(FileUtils.readFileToByteArray(file)));
	CloseableHttpClient httpClient = HttpClientBuilder.create().build();
	HttpResponse httpResponse = httpClient.execute(httpPost);
	int statusCode = httpResponse.getStatusLine().getStatusCode();
	System.out.println(statusCode + ':'+ httpResponse.getStatusLine().getReasonPhrase());

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 ?


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, get the resulting ID, and attach the ID to the status.