{"id":6305,"date":"2012-08-24T16:12:56","date_gmt":"2012-08-24T10:42:56","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=6305"},"modified":"2017-02-06T10:40:25","modified_gmt":"2017-02-06T05:10:25","slug":"login-with-twitter","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/login-with-twitter\/","title":{"rendered":"Login with Twitter"},"content":{"rendered":"<p>This application saves user\u2019s time for registration by providing a way through which user can login into application with its twitter credentials. Application will get the user details from his corresponding twitter account and will do the registration programmatically, so user can easily manage account through his twitter account.<\/p>\n<h5>Five simple steps to connect with twitter<\/h5>\n<p><strong>1. <\/strong>First of all, you have to <a href=\"https:\/\/dev.twitter.com\/apps\">register<\/a> your application with twitter. Once the application is registered you will receive <strong>\u201cconsumer key\u201d<\/strong> and <strong>\u201cconsumer secret\u201d<\/strong>.<\/p>\n<p><strong>2.<\/strong> Write <strong>\u201cconsumer key\u201d<\/strong> and <strong>\u201cconsumer secret\u201d<\/strong> in <strong>Config.groovy<\/strong> as:<\/p>\n<p>[groovy]<br \/>\ntwitter {<br \/>\n    oauth.consumer_key = &#8216;mjnSnJAtlpI0TTriR4gQ&#8217;<br \/>\n    oauth.consumer_secret = &#8217;86IEve8R37TNAIoSFoGPlMxbPCHpvvmFr2eAwwU&#8217;<br \/>\n}<br \/>\n[\/groovy]<\/p>\n<p><strong>3.<\/strong> To provide <strong>twitter<\/strong> utility into <strong>grails<\/strong> app, add dependance into <strong>BuildConfig.groovy<\/strong> as:<\/p>\n<p>[groovy]<br \/>\nbuild &#8216;net.homeip.yusuke:twitter4j:2.0.10&#8217;<br \/>\n[\/groovy]<\/p>\n<p><strong>Twitter<\/strong> provide <strong>twitter4j<\/strong> named API to intract with twitter.<\/p>\n<p><strong>4.<\/strong> Create a link which says to <strong>login with twitter<\/strong> as:<\/p>\n<p>[groovy]<br \/>\nLogin with Twitter<br \/>\n[\/groovy]<\/p>\n<p><strong>5.<\/strong> Create <strong>LoginController<\/strong> with <strong>actionName<\/strong> as <strong>loginWithTwitter<\/strong>:<\/p>\n<p>[groovy]<br \/>\nimport org.codehaus.groovy.grails.commons.ConfigurationHolder<\/p>\n<p>class LoginController {<\/p>\n<p>    def requestLogin() {<br \/>\n        def twitter = new twitter4j.Twitter()<br \/>\n        def callbackUrl = g.createLink(action: &#8216;processLogin&#8217;, absolute:true).toString()<br \/>\n        def consumerKey = ConfigurationHolder.config.twitter.oauth.consumer_key<br \/>\n        def consumerSecret = ConfigurationHolder.config.twitter.oauth.consumer_secret<br \/>\n        twitter.setOAuthConsumer(consumerKey, consumerSecret)<br \/>\n        def requestToken = twitter.getOAuthRequestToken(callbackUrl)<br \/>\n        session.twitter = twitter<br \/>\n        session.requestToken = requestToken<br \/>\n        redirect(url:requestToken.getAuthorizationURL())<br \/>\n    }<\/p>\n<p>    def processLogin() {<br \/>\n        if (!session.requestToken) {<br \/>\n            redirect(action: &#8216;requestLogin&#8217;)<br \/>\n        } else {<br \/>\n            def accessToken = session.twitter.getOAuthAccessToken(session.requestToken, params.oauth_verifier)<br \/>\n            def twitterUser = session.twitter.verifyCredentials()<br \/>\n            session.twitterUser = twitterUser<br \/>\n            redirect(action: &#8216;dashboard&#8217;)<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\n[\/groovy]<\/p>\n<p>When user click on Login with Twitter link, requestLogin action is called which redirects request to twitter for authorization. On successful authorization from twitter, user will be redirected to call back url (on processLogin Action). processLogin Action gets all the information related to user (in twitterUser object) from twitter. Registration can be done for that user with that twitter information, and user can then access the application through his twitter account information.<\/p>\n<p><strong><a href=\"http:\/\/www.tothenew.com\/blog\/author\/amit-kumar\/\">More Blogs by Me<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This application saves user\u2019s time for registration by providing a way through which user can login into application with its twitter credentials. Application will get the user details from his corresponding twitter account and will do the registration programmatically, so user can easily manage account through his twitter account. Five simple steps to connect with [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[7],"tags":[4840,878,873],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6305"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=6305"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6305\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=6305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=6305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=6305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}