{"id":12721,"date":"2014-04-04T01:09:26","date_gmt":"2014-04-03T19:39:26","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=12721"},"modified":"2016-12-19T15:16:43","modified_gmt":"2016-12-19T09:46:43","slug":"using-the-instagram-login-api-via-grails","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/using-the-instagram-login-api-via-grails\/","title":{"rendered":"Using the Instagram Login API via Grails"},"content":{"rendered":"<p>In my current web application, I had to integrate login functionality via Instagram. To do so, I had to go through the following steps:<\/p>\n<p>1. First you have to register your application using this <a title=\"Instagram App Registration\" href=\"http:\/\/instagram.com\/developer\/register\/\">link<\/a>. In order to register your application, you need to have an Instagram account which can be done through Instagram&#8217;s mobile application.<\/p>\n<p>2.After registering your application you will get the following credentials:<br \/>\n&#8211; <em><strong>client_id<\/strong><\/em><br \/>\n&#8211; <em><strong>client secret<\/strong><\/em><\/p>\n<p>3. Once you have the required access credentials for your app, login into Instagram using the following piece of code:<\/p>\n<p>[code]<br \/>\ndef loginInstagram() {<br \/>\n String redirectUri = &#8216;http:\/\/abc.com:8080\/sample\/instagramAnalysis\/instagramCallBack&#8217;<br \/>\n String clientId = &amp;quot;123456778909jnjfnjnjfrgnjgn&amp;quot;<br \/>\n String oAuthUrl=&amp;quot;https:\/\/api.instagram.com\/oauth\/authorize\/?client_id=&amp;quot;<br \/>\n String instagramOAuthUrl = [oAuthUrl, clientId, &amp;quot;&amp;amp;redirect_uri=&amp;quot;, redirectUri, &amp;quot;&amp;amp;response_type=code&amp;quot;].join(&amp;quot;&amp;quot;)<br \/>\n redirect(url: instagramOAuthUrl)<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>Here <em><strong>redirectUri<\/strong><\/em> refers to the URI of our application, where we will access response from Instagram&#8217;s API once the credentials are verified and login is successful.<\/p>\n<p>4. On successful login, Instagram provides a <em><strong>code<\/strong><\/em>\u00a0parameter in the response. Once we have the <em><strong>code\u00a0<\/strong><\/em>parameter, it can be used to get an\u00a0<em><strong>access_token\u00a0<\/strong><\/em>by making a post request to the URL . This URL provides us a response in JSON format which contains the logged in user&#8217;s information. You are now supposed to save this <em><strong>access_token<\/strong><\/em> in order to fetch Instagram data by making different API calls.<\/p>\n<p>[code]<br \/>\ndef instagramCallBack() {<br \/>\n   User currentUser = User.findUserByUserName(springSecurityService.c urrentUser.username)<br \/>\n   String clientId = &amp;quot;123456778909jnjfnjnjfrgnjgn&amp;quot;<br \/>\n   String clientSecret = &#8216;123456778909jnjefjefhejhrfjehrejejjfnjnjfrgnjgn&#8217;<br \/>\n   String redirectUri = &#8216;http:\/\/abc.com:8080\/sample\/instagramAnalysis\/instagramCallBack&#8217;<br \/>\n   String accessTokenUrl = &amp;quot;https:\/\/api.instagram.com\/oauth\/access_token&amp;quot;<\/p>\n<p>   Map accessTokenMap = [<br \/>\n                        client_id: clientId,<br \/>\n                        client_secret: clientSecret,<br \/>\n                        grant_type: &#8216;authorization_code&#8217;,<br \/>\n                        redirect_uri: redirectUri,<br \/>\n                        code: params.code<br \/>\n                     ]<br \/>\n   JSONObject instagramJson = fetchInstagramAccessToken(accessTokenUrl, accessTokenMap) \/\/ extract access token from URL<br \/>\n   render instagramJson<br \/>\n }<br \/>\n[\/code]<\/p>\n<p>To learn more about it you can follow the link <a href=\"http:\/\/instagram.com\/developer\">http:\/\/instagram.com\/developer<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my current web application, I had to integrate login functionality via Instagram. To do so, I had to go through the following steps: 1. First you have to register your application using this link. In order to register your application, you need to have an Instagram account which can be done through Instagram&#8217;s mobile [&hellip;]<\/p>\n","protected":false},"author":72,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":6},"categories":[7],"tags":[4840,1368,878],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/12721"}],"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\/72"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=12721"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/12721\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=12721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=12721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=12721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}