Twitter4J authentication


#1

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 twitter4j.properties for the key/secret. I’ve got the twitter4j.properties 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.


#2

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 “http://api.twitter.com/oauth/authorize?oauth_token=TOKEN”) and send that value back to the frontend for redirection (note: the consumer_key and secret now seem to be loading from twitter4j.properties 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 (https://dev.twitter.com/pages/auth) 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:
http://www.google.co.jp/search?q=bfb606ed or
http://www.google.co.jp/search?q=4ef9707c
TwitterException{exceptionCode=[bfb606ed-4ef9707c], statusCode=401, message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=3.0.3}
at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:177)
at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:61)
at twitter4j.internal.http.HttpClientWrapper.post(HttpClientWrapper.java:98)
at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:122)
at twitter4j.auth.OAuthAuthorization.getOAuthRequestToken(OAuthAuthorization.java:104)
at twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:281)
at twitter4j.TwitterBaseImpl.getOAuthRequestToken(TwitterBaseImpl.java:273)

Why?


#3

I ended up bailing on twitter4j and doing it manually. I detailed my java solution to the request_token flow here: https://dev.twitter.com/discussions/13935


#4

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?


#5

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