Day by day, social networking sites usage is becoming more. The web sites or the mobile apps are leveraging the social networking sites sign-in functionality to log in to their systems. In this article, we will see ‘how to set up twitter sign in for applications’ using twitter-4j by following the below steps.
Step 1: Create an application in https://dev.twitter.com/ by proving the application name, description, website URL and the callback URL(see the below screenshot). The callback URL is required to redirect the application, once the authentication is successful with Twitter.
Step 2: After creating the application, go to the application and change the settings(choose the “Access” and check “Allow this application to use the sign in with twitter option).
Step 3: Go to the application and collect the “Consumer key” and “Consumer secret“.
Step 4: Download the twitter4j distribution from http://twitter4j.org/en/ and put twitter4j-core-3.0.3.jar in the class-path.
Step 5: Write a Servlet or JSP to use as part of “Callback URL“. In this example, we will go with JSP(In the step1 screenshot, you can see the callback URL pointing to the same JSP).
function closeWindow(){ self.close(); }
Step 6: Write a Servlet called “TwitterSigninServlet”. This Servlet will get called when the user clicks on the button. The Servlet code is given below,
package org.smarttechie.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import twitter4j.Twitter; import twitter4j.TwitterException; import twitter4j.TwitterFactory; import twitter4j.auth.RequestToken; /** * Servlet implementation class TwitterSigninServlet */ public class TwitterSigninServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public TwitterSigninServlet() { super(); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { Twitter twitter = new TwitterFactory().getInstance(); RequestToken requestToken; request.getSession().setAttribute("twitter", twitter); twitter.setOAuthConsumer("", ""); requestToken = twitter.getOAuthRequestToken("http://127.0.0.1:8080/Twitter-Sign-In-Web/views/redirect.jsp"); String authURL = requestToken.getAuthenticationURL(); request.getSession().setAttribute("requestToken", requestToken); response.sendRedirect(authURL); } catch (TwitterException twitterException) { twitterException.printStackTrace(); } } }
Step 7: Write a JSP by putting “Sign in with Twitter” image. When the user clicks that image, we will call /signin Servlet(Created in Step6).
The sample twitter login testing <a href="loginTwitter()"><img alt="twitter login" src="images/sign-in-with-twitter-gray.png" /></a>
In the above JSP, we have included a JS file called “twitter-login.js”. The code is given below.
var width = 820; var height= 580; var left = (screen.width/2)-(width/2); function loginTwitter() { window.open("http://127.0.0.1:8080/Twitter-Sign-In-Web/signin", "mywindow","menubar=0,resizable=0,scrollbars=no,width="+width+",height="+height+",status=0,toolbar=0,left="+left+",top=120"); }
Once the user clicks on “Sign in with Twitter”, the Twitter Sign in prompt will appear. Once the user gets authorized with Twitter, the request will redirect to the “Callback URL“. Inside the “Callback URL”, we can get the authorized Twitter user id. Based on that, we can get the entire user object(look at Step5).
The source code available here.
thanks you very much
it’s very helpfull
Thank you! This really helped me.
Great article.
Hello, just wanted to mention, I loved this blog post. It was practical.
Keep on posting!
This website is awesome…
Thanks for sharing your thoughts. I really appreciate your efforts and I am
waiting for your further post thanks once again.
I like the valuable information you provide in your articles.
I’ll bookmark your blog and check again here regularly. I am quite certain I’ll learn lots of
new stuff right here! Good luck for the next!
Greetings I am so delighted I found your webpage, I really found you
by error, while I was researching on Askjeeve for something else, Regardless I am here now and would just like to
say kudos for a fantastic post and a all round exciting blog (I
also love the theme/design), I don’t have time to
go through it all at the minute but I have bookmarked it and also added
in your RSS feeds, so when I have time I will be back
to read more, Please do keep up the awesome job.
Magnificent website. Plenty of useful information here.
thanks for your effort!
Thanks for finally talking about >Sign in with twitter using Twitter4j | Smart Techie <Liked it!
What’s up, I read your new stuff daily. Your story-telling style is awesome, keep doing what you’re doing!
Have you ever considered about including a little bit more than just
your articles? I mean, what you say is important and all.
Nevertheless think of if you added some great
graphics or video clips to give your posts more, “pop”!
Your content is excellent but with pics and clips, this website could definitely
be one of the best in its niche. Great blog!
Thanks for your marvelous posting! I actually enjoyed reading it,
you will be a great author.I will remember to bookmark your blog and may come back from now on.
I want to encourage one to continue your great posts, have a nice morning!
This website is Relevant!!
I have found something which helped me.
Thanks!