I want to access the stream API in java. I created an app named MyBigDataCommunity with all the parameter required for it. But I got the following error message after running the code given below. Please help me to access the stream API.
local time isThu Feb 27 14:16:57 UTC 2014
Thu Feb 27 14:16:57 UTC 2014
HTTP ERROR: 401
Unauthorized
----------------------------------------
----------------------------------------
BUILD SUCCESSFUL (total time: 3 seconds)
public static void main(String[] args) throws Exception {
final String accept="*/ ";
final String accepttitle="Accept ";
final String conection="Keep_Alive";
final String contitle="Connection";
final String userAgent =" OAuth gem v0.4.4";
final String usertitle="User-Agent";
final String Authorization = "Authorization: OAuth oauth_consumer_key=\"3413uV4NL3xIZg2c1qHq6Q\", oauth_nonce=\"724d8070fd3a52823af0d12d40872872\", oauth_signature=\"Zn%2F6WmwwCSyuvjznoB8PETAgJaM%3D\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"1393510233\", oauth_token=\"1891307940-Rykqh0Sw359QEjNXLUSctOrDP0oYVlyW7AR77nP\", oauth_version=\"1.0\" ";
final String headerTitle = "Authorization";
final String contentLenth="76 ";
final String contentitle="Content-Length";
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost post=new HttpPost("https://stream.twitter.com/1.1/statuses/filter.json");
post.addHeader(accepttitle,accept);
post.addHeader(contitle,conection);
post.addHeader(usertitle,userAgent);
post.addHeader(headerTitle, Authorization);
HttpResponse response = httpclient.execute( post );
System.out.println("local time is"+new Date());
try {
StringEntity postEntity = new StringEntity("UTF-8");
postEntity.setContentType("application/x-www-form-urlencoded");
HttpEntity entity = response.getEntity();
post.setEntity(postEntity);
Date date=new Date();
String i=String.valueOf(date);
String datetitle=" Date";
response.addHeader(datetitle,i);
System.out.println(date);
post.setEntity(entity);
if (postEntity != null)
{
long len = entity.getContentLength();
if (len != -1 && len < 2048)
{
System.out.println(EntityUtils.toString(entity));
}
else
{
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String line = null;
StringBuffer buffer = new StringBuffer();
while ((line = reader.readLine()) != null) {
buffer.append(line);
if(buffer.length()>30000) break;
String byteOne = reader.readLine();
// String byteTwo = reader.readLine();
System.out.println(byteOne );
}
}
}
// ResponseHandler<String> responseHandler = new BasicResponseHandler();
// String responseBody = httpclient.execute(post, responseHandler);
System.out.println("----------------------------------------");
// System.out.println(responseBody);
System.out.println("----------------------------------------");
} catch (HttpResponseException e) {
System.out.println("Reponse status code: " + e.getStatusCode()
+ "\n");
e.printStackTrace();
httpclient.getConnectionManager().shutdown();
}
}