Oauth authentication with Twitter4j Not working for me


#1

Hi Iam using the exact code from this Tutorial

This is the code of my two servlets
LoginServlet.java

try {
TwitterFactory factory = new TwitterFactory();
Twitter twitter = factory.getInstance();
twitter.setOAuthConsumer(Consumer_Key,Consumer_Secret);
RequestToken requestToken = twitter.getOAuthRequestToken();
String token = requestToken.getToken();
String tokenSecret = requestToken.getTokenSecret();
session.setAttribute(“token”, token);
session.setAttribute(“tokenSecret”, tokenSecret);
String authUrl = requestToken.getAuthorizationURL();

        request.setAttribute("authUrl", authUrl);
                  
        RequestDispatcher rd = request.getRequestDispatcher("twitter.jsp");
        rd.forward(request, response);
             
        }catch(Exception e){
            out.println(e.getMessage());
        }

and
HomeServlet.java (as callback URL)

TwitterFactory factory = new TwitterFactory();

Twitter twitter = factory.getInstance();

twitter.setOAuthConsumer(Consumer_Key, Consumer_Secret);

    AccessToken accessToken;
    try {
        accessToken = twitter.getOAuthAccessToken((String) session.getAttribute("token"), (String) session.getAttribute("tokenSecret"));
        twitter.setOAuthAccessToken(accessToken);

        User user = twitter.verifyCredentials();
        
        out.println(user.getId()); 
        out.println(user.getMiniProfileImageURL()); 
    
    } catch (TwitterException ex) {
        
        out.println("************************************");
        out.println(ex.getMessage());
        out.println(ex.getErrorMessage());
        out.println("************************************");
        
        //Logger.getLogger(TwitterLogin.class.getName()).log(Level.SEVERE, null, ex);
    }

Here Iam getting the response from callback url is

“401:Authentication credentials (https://dev.twitter.com/docs/auth) were missing or incorrect. Ensure that you have set valid conumer key/secret, access token/secret, and the system clock in in sync.
The screen name / password combination seems to be invalid.”

Please help how to solve this. I had already set the Application Type as Read, Write and Access direct messages.


#2

Tweet posted on facebook