Twitter4J can't Authenticate

twitter4j
java

#1

Hi, I think my code is all right but I keep getting the following error when I try to post a message:

[TwitterException: 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.
message - Invalid or expired token.
code - 89
]

Here is my code:

package controllers;

import play.*;
import play.mvc.*;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import twitter4j.conf.*;
import views.html.*;
import twitter4j.Twitter;

import java.io.*;

public class Application extends Controller {

    public Result index() throws TwitterException {
        ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();

        if (session("authToken") == null ) {
            // configure
            configurationBuilder.setOAuthConsumerKey(<MY_CONSUMER_KEY>);
            configurationBuilder.setOAuthConsumerSecret(<MY_CONSUMER_SECRET>);
            twitter4j.conf.Configuration configuration = configurationBuilder.build();

            Twitter twitter = new TwitterFactory(configuration).getInstance();
            try {
                String callback = routes.Application.twitterLoginCallback().absoluteURL(request());
                RequestToken requestToken = twitter.getOAuthRequestToken(callback);
                session("requestToken", requestToken.getToken());
                session("requestTokenSecret", requestToken.getTokenSecret());

                return redirect(requestToken.getAuthorizationURL());

            } catch(Exception ex) {}
        } else {

            configurationBuilder.setOAuthConsumerKey(<MY_CONSUMER_KEY>);
            configurationBuilder.setOAuthConsumerSecret(<MY_CONSUMER_SECRET>);
            configurationBuilder.setOAuthAccessToken(session("authToken"));
            configurationBuilder.setOAuthAccessTokenSecret(session("authSecret"));
            twitter4j.conf.Configuration configuration = configurationBuilder.build();

            Twitter twitter = new TwitterFactory(configuration).getInstance();
            twitter.updateStatus("Hello world!");
        }

        return ok(index.render());
    }

    public Result twitterLoginCallback() throws TwitterException {
        Twitter twitter = new TwitterFactory ().getInstance();
        twitter.setOAuthConsumer(<MY_CONSUMER_KEY>, <MY_CONSUMER_SECRET>);
        AccessToken accessToken = new AccessToken(session("requestToken"), request().getQueryString("oauth_verifier"));

        session("authToken", accessToken.getToken());
        session("authSecret", accessToken.getTokenSecret());

        return redirect(routes.Application.index());
    }


}

#2

I believe the issue is you are not getting an AccessToken and instead are just using the request token details. Have a look at getOAuthAccessToken for exchanging the request token for an access token.