401 response status


I’ve been getting 401 while using the streaming API trying to authenticate using OAuth ever since the OAuth tool went down and came back up again. My code was working before and I havent changed anything in it. Should I be changing some request parameters?

This is my code:

public static final String AUTHORIZE_URL = “https://api.twitter.com/oauth/authorize”;
public static final String REQUEST_TOKEN_URL = “https://api.twitter.com/oauth/request_token”;
public static final String ACCESS_TOKEN_URL = “https://api.twitter.com/oauth/access_token”;
HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, new Integer(Settings.HTTP_CONNECTION_TIMEOUT));
OAuthConsumer consumer = new CommonsHttpOAuthConsumer(OAuthUtils.CONSUMER_KEY,OAuthUtils.CONSUMER_SECRET);
List params = new ArrayList();
params.add(CreateNameValuePair(“track”, “twitter”));
baseUrl = “https://stream.twitter.com/1.1/statuses/filter.json”;
HttpPost httppost = new HttpPost(baseUrl);
httppost.setEntity(new UrlEncodedFormEntity(params, “UTF-8”));
HttpResponse response = httpClient.execute(httppost);

Variable response is

HTTP/1.1 401 Authorization Required [Cache-Control: must-revalidate,no-cache,no-store, connection: close, content-length: 266, Content-Type: text/html, date: Fri, 03 Oct 2014 15:56:01 UTC, server: tsa, WWW-Authenticate: OAuth realm="Firehose", x-connection-hash: 5b928faac0eba33942e0166100cf1dd4]

Same code worked before.
Any help will be appreciated. Thanks!


Have you tried to regenerate your consumer key and consumer secret? Also, what value did you use for your call back url?


Yes. I tried regenerating the keys and I haven’t set any callback url. I get the access token and secret using Out of Band OAuth.


Just wanted to add that the same OAuth keys seem to work with the REST API.


Ok. Can you also check if you can use the curl command tool mentioned under “Test OAuth” listed inside of your app? It should give you an example cURL command that you can copy and paste on command line for the relevant API. This will help you make sure your Twitter OAuth credentials are correct.


curl --get ‘https://api.twitter.com/1.1/search/tweets.json’ --data ‘q=twitter’ --header ‘Authorization: OAuth oauth_
consumer_key="…", oauth_nonce="…", oauth_signature="…", oauth_signature_method=“HMAC-SHA1”, oauth_timestamp=“1412362701”, oauth_token="…", oauth_version=“1.0”’ --verbose

returns 200 OK but

curl --request ‘POST’ ‘https://stream.twitter.com/1.1/statuses/filter.json’ --data ‘track=twitter’ --header ‘Authorization: OAuth oauth_consumer_key="…", oauth_nonce="…", oauth_signature="…", oauth_signature_method=“HMAC-SHA1”, oauth_timestamp=“1412362857”, oauth_token="…", oauth_version=“1.0”’ --verbose

returns 401


That is interesting. I tried the POST request with the same API and I am able to establish a streaming API connection and stream data.

curl --request 'POST' 'https://stream.twitter.com/1.1/statuses/filter.json' --data 'track=twitter' --header 'Authorization: OAuth oauth_consumer_key="xxxxx", oauth_nonce="xxxxx", oauth_signature="xxxxxxx", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1412369025", oauth_token="xxxx-xxxxx", oauth_version="1.0"' --verbose



Could you share your code as radha_cr did above?