Twitter4J authentication


Everyone (and especially Yasuke),

I’m attempting to follow the instructions here…

… but failing due to the persistent nature of Twitter4J and my use of it in a web services endpoint.

On the first call from a user to the backend to startTwitterAuthentication (my method), everything works fine as I instantiate the Twitter object, set the consumer key and secret, and get request token. I retrieve the redirect url and sent the user there where the PIN number appears (which my application can programmatically extract). So far so good…

The trouble is with the second request. I get: “consumer key/secret pair already set” The Twitter object doesn’t want to be instantiated again even though it appears to be a a local variable in my scope. This would be easy to skip with an “if (key/secret pair is already set)” conditional, but I can’t find such a function within the Twitter class. Please help me understand where I’m going wrong on this. I’ve tried to move the Twitter object into a more global scope, but I get errors there, too (which, unfortunately, I can’t remember – exhausted from doing battle with Twitter API all day).

My ultimate goal is to get access tokens for users, put them in their cookies and have the users make twitter search api requests within my frontend. This is SUPER easy right now using API 1.0 because no authentication is required. Hence, the need for twitter4j.

In addition to the above problem, I’m uncertain how to use for the key/secret. I’ve got the file in the right place, but the example code above only shows how to use them hardcoded with twitter.setOAuthConsumer(“FasdfasdfasdfadsfasdfasfaQ”, “2pBLAHBLAHBLAHBALHBLHABHL4”). Is there a way to instantiate the Twitter object and tell it “use the properties file”?

Thanks for your help.


Let me clarify the above. My app has a web services endpoint the html/css/js frontend talks to. This endpoint is written in java and takes GET requests with a “method” value. The “function” in question is as follows. It is supposed to use Twitter4j to get the redirect_url (in the form “”) and send that value back to the frontend for redirection (note: the consumer_key and secret now seem to be loading from without hardcode inclusion in this java code):

else if(method.equals("startTwitterAuthentication")) { System.out.println("endpoint startTwitterAuthentication entering"); // The factory instance is re-useable and thread safe. Twitter twitter = TwitterFactory.getSingleton(); RequestToken requestToken; String redirect_url = ""; try { requestToken = twitter.getOAuthRequestToken(); redirect_url = requestToken.getAuthorizationURL(); } catch (TwitterException e) { // TODO Auto-generated catch block e.printStackTrace(); } //persist to the accessToken for future reference. // storeAccessToken(twitter.verifyCredentials().getId() , accessToken); if(redirect_url.equals("")) { jsonresponse.put("response_status", "error"); jsonresponse.put("message", "Couldn't get redirect_url from twitter"); } else { jsonresponse.put("response_status", "success"); jsonresponse.put("redirect_url", redirect_url); } System.out.println("endpoint startTwitterAuthentication exiting"); }

The first go-around, it does this correctly and the user is directed to the Twitter page asking if permissions should be granted. If I (the user) close the tab of this redirect or otherwise exit the process and try to start it again, (startTwitterAuthentication called a second time) this happens:

endpoint startTwitterAuthentication entering 401:Authentication credentials ( were missing or incorrect. Ensure that you have set valid consumer key/secret, access token/secret, and the system clock is in sync. Failed to validate oauth signature and token

Relevant discussions can be found on the Internet at: or
TwitterException{exceptionCode=[bfb606ed-4ef9707c], statusCode=401, message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=3.0.3}
at twitter4j.internal.http.HttpClientImpl.request(
at twitter4j.internal.http.HttpClientWrapper.request(
at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(
at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(
at twitter4j.TwitterBaseImpl.getOAuthRequestToken(
at twitter4j.TwitterBaseImpl.getOAuthRequestToken(



I ended up bailing on twitter4j and doing it manually. I detailed my java solution to the request_token flow here:


can you help me? i have a problem with the codes:

twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(“my key”, “my secret”);
RequestToken requestToken = twitter.getOAuthRequestToken();

but it goes wrong with the last code, show errors “Received authentication challenge is null…”

do you know why?


As I can log in through twitter on my website, some example? please