Working with curl, but not with HttpClient


#1

Hi there,

I’m currently trying to connect to streaming endpoint with using OAuth.

I created my tokens etc and using oauth tool from project development page, as you know it produces 3 parameters, namely:

Signature Base String
Authorization header
curl command

I copied and pasted curl command to my terminal, and It worked, getting stream.
However when It comes to my very simple java program below, weirdly response always returned 401 Unauthorized.

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(“https://stream.twitter.com/1.1/statuses/filter.json”);

  BasicHttpParams parameters = new BasicHttpParams();
  parameters.setParameter("track", "twitter");

  try {
     // HERE I HAVE AUTHORIZATION HEADER COPIED FROM OAUTH TOOL
     httpPost.setHeader("Authorization", "OAuth oauth_consumer_key=\"4ad1COZ...");

     httpPost.setParams(parameters);

  } catch(Exception e) {
     e.printStackTrace();
  }

  try {
     HttpResponse httpResponse = httpClient.execute(httpPost);
     System.out.println(httpResponse.getStatusLine());
     HttpEntity httpEntity = httpResponse.getEntity();
     ///
     BufferedReader br = new BufferedReader(new InputStreamReader(httpEntity.getContent()));
     String line;
     while((line=br.readLine())!=null){
        System.out.println(line);
     }
     ////
     EntityUtils.consume(httpEntity);

  } catch(UnsupportedEncodingException e) {
     e.printStackTrace();
  } catch(ClientProtocolException e) {
     e.printStackTrace();
  } catch(IOException e) {
     e.printStackTrace();
  }finally {
     httpPost.releaseConnection();
  }</java>

Please help, I’m digging into that simple thing for over a week, finding no answer at all.