Friday, 23 November 2012

Twitter Log in on your website with send a Tweet functionality using OAuth 2.0 and Twitter4j


This script
- authenticates users on your website using their Twitter credentials.
- allows users to post a Tweet via your website.
- pulls the user details from its Twitter account and displays the same on your webpage.
- has logout facility and uses Java servlets.




Download Pack     Live Demo    


How to use:

The download pack contains the complete project structure with the sample war file that I have deployed in my live demo.

1. Create a Twitter Application

Create your new Twitter application here. Enter your website URL that needs the Twitter sign in functionality.

Also enter a callback URL. (We can change the callback value from our code but make sure some dummy value is entered here). Once the user is authenticated by Twitter SignIn on your webpage, the control will be sent to this URL. In our case the callback URL is a servlet that will forward the control to our JSP.



Note the ConsumerKey and ConsumerSecret of your Twitter app.



In the settings tab of your Twitter Application, set the Application Type to the value "Read, Write and Access direct messages".



2. Adjust Server Timezone

Make sure your application server timezone and the Twitter server timezone are in sync. Now this can be tricky. Generally the Twitter Oauth servers are in sync with GMT/UTC time. So if you have access just set your server time to GMT+0 timezone.

I used a Jboss app server in Linux environment and did not have permissions to update server time. So I used the command export _JAVA_OPTIONS="-Duser.timezone=Europe/London" that brings Jboss server time to GMT timezone. (Restart Jboss after executing the command)

3. Web.xml (Input variables)

Specify the ConsumerKey and ConsumerSecret from your Twitter app in your web.xml

After successful login index.jsp will be shown to the user.



4. Index.jsp (Output variables)

The user account details will be available as session attributes in index.jsp
The object twitterUser contains the user attributes as shown. You may write your own display code instead, as required.


Let me know if you face any issues.

13 comments:

  1. Greetings, I need some help, I have created a third-party application to twitter and all goes well until I get the pop up asking for permission and acceptance of the application, but if I give in some computer work and others do not redirects me to the page, and stays the pop up with a redirect message, not advance or get an error .. twttetr am using the library to do this but it does not work, I appreciate the answers you can give me ..

    ReplyDelete
  2. Replies
    1. nah, using openshift .. its a gud one, probably you can shift to that

      Delete
  3. All you need to do is, download the zip file, update your keys in web.xml and if u want update the section in index.jsp

    This should work..

    ReplyDelete
  4. Hi i was going through the Live demo above.i am getting the login page for twitter and once i login with twitter credentials its giving the authorization page and when i click on sign in button its giving HTTP Status 500 -

    Thanks

    ReplyDelete
    Replies
    1. Okay problem solved!
      The reason is that some people have their website as blank, hence the exception.

      Anyway, I have updated the Demo and the download code. Happy coding :)

      Delete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Hi i'm targeting the url like this www.mawk3y.net/twitter/WebContent/index.jsp and i get the inner code in the page as i'm viewing source and it doesn't work at all

    ReplyDelete
    Replies
    1. Hi

      May I know what application server you are using.. ?

      You need to deploy your code on some application server like Jboss/
      Tomcat etc..

      Then you can call your login page - www.mawk3y.net/TweetLogin

      Delete
    2. i'm not using any application it's a php server and i was testing this code to use it

      Delete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Hello,

    Great example. Also, it is good that there is a live demo and the source code is available for download.

    I downloaded the source code and tried to execute it locally. Unfortunately, it did not work. Then, I tried the live demo as well. It did not work too. Here is what happens:

    I click on the "Login With Twitter" button. I get redirected to Twitter. I authorize my application. Then Twitter redirects back to the callback URL. However, then the application returs "Failed to connect to the Twitter API" error. Can you check what the problem is and why this is now working?

    Regards,
    Rashid

    ReplyDelete
  9. live demo not working

    ReplyDelete