{"id":4687,"date":"2011-12-06T12:42:04","date_gmt":"2011-12-06T07:12:04","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=4687"},"modified":"2016-12-19T15:06:34","modified_gmt":"2016-12-19T09:36:34","slug":"integrating-google-plus-in-grails-application","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/integrating-google-plus-in-grails-application\/","title":{"rendered":"Integrating Google plus in grails application"},"content":{"rendered":"<p>In my current project, i needed to integrate Google+ in the application using server-side API. Google uses OAuth2.0 protocol for authorization when our application tries to access the data. All we require is an access token to fetch data from Google using REST calls which serves data in JSON format.<\/p>\n<p>I implemented it using Web Server Applications API and thought it worth sharing.<br \/>\nThere are basically 3 steps to fetch data from Google.<br \/>\n<strong>1. Register an application.<\/strong><\/p>\n<p>We need to register an application at <a href=\"https:\/\/code.google.com\/apis\/console\">Google API Console<\/a>. Go to the Google console page using the link provided and create an aplication.<\/p>\n<p>Steps involve in registering an application are:-<\/p>\n<p>a.) Create project by providing name to the project.<\/p>\n<p>b.) Turn ON the service required, in our case it is Google Plus API.<\/p>\n<p>c.) Create a Oauth 2.0 Client ID.<\/p>\n<p>d.) Create the OAuth 2.0 ClientId and provide the callback URL where google will send the authorization token.<\/p>\n<p>e.) Note down Client ID and Client Secret as generated in the above step.<\/p>\n<p><strong>2. Obtain an Access Token from the Google Authorization Server.<\/strong><\/p>\n<p>Obtaining an access token involves 2 steps.<\/p>\n<p>a.) Request for Authorization Code.<\/p>\n<p>In this step, we will request the Google server for authorization code by providing registered application client ID in the URL to Google server.<\/p>\n<p>I created an action to redirect to Goolge, when someone want to connect to google plus.<\/p>\n<p>[java]<br \/>\nString CLIENT_ID =client_id_obtained from registered app<br \/>\nString CALLBACK_URL = callback_url_as_mentioned in the registered app.<br \/>\nString GOOGLE_PLUS_SCOPE=&#8217;https:\/\/www.googleapis.com\/auth\/plus.me&#8217;    \/\/ scope is the permissions we are requesting.<br \/>\n[\/java]<\/p>\n<p>Action code is as:<\/p>\n<p>[java]<br \/>\ndef registerOnGooglePlus = {<br \/>\nString authorizeUrl = &amp;amp;amp;amp;amp;quot;https:\/\/accounts.google.com\/o\/oauth2\/auth?scope=${GOOGLE_PLUS_SCOPE}&amp;amp;amp;amp;amp;amp;<br \/>\nredirect_uri=${CALLBACK_URL}&amp;amp;amp;amp;amp;amp;response_type=code&amp;amp;amp;amp;amp;amp;client_id=${CLIENT_ID}&amp;amp;amp;amp;amp;amp;access_type=offline&amp;amp;amp;amp;amp;quot;<br \/>\nURL urlForGooglePlus = new URL(authorizeUrl)<br \/>\nredirect(url: urlForGooglePlus)<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n<p>The Redirect will take user to permissions page, if the user is already logged-in or will take to login page and then permissions page.<\/p>\n<p>After approving the required permissions, user will redirect back to the application&#8217;s registered Callback URL with the authorization code.<\/p>\n<p>b.) Request for Access Token with the authorization code obtained from the above action.<\/p>\n<p>Access Token can be received by a POST request using the Client Secret and authorization code received.<\/p>\n<p>The POST call requires 5 properties to be send in the body of the request in the encoded form.<\/p>\n<p>[html]<\/p>\n<p>\tcode : The authorization code returned from the initial request<br \/>\n\tclient_id : The client_id obtained during application registration<br \/>\n\tclient_secret : The client secret obtained during application registration<br \/>\n\tredirect_uri : The URI registered with the application<br \/>\n\tgrant_type : authorization_code<br \/>\n[\/html]<\/p>\n<p>[java]<\/p>\n<p>\/\/ Sample action to receive authorization code<\/p>\n<p>def callBack={<br \/>\nStringBuilder sb = new StringBuilder(&amp;amp;amp;amp;amp;quot;code=&amp;amp;amp;amp;amp;quot;);<br \/>\nsb.append(URLEncoder.encode(code, &amp;amp;amp;amp;amp;quot;UTF-8&amp;amp;amp;amp;amp;quot;));<br \/>\nsb.append(&amp;amp;amp;amp;amp;quot;&amp;amp;amp;amp;amp;amp;client_id=&amp;amp;amp;amp;amp;quot;);<br \/>\nsb.append(URLEncoder.encode(clientId, &amp;amp;amp;amp;amp;quot;UTF-8&amp;amp;amp;amp;amp;quot;));<br \/>\nsb.append(&amp;amp;amp;amp;amp;quot;&amp;amp;amp;amp;amp;amp;client_secret=&amp;amp;amp;amp;amp;quot;);<br \/>\nsb.append(URLEncoder.encode(clientSecret, &amp;amp;amp;amp;amp;quot;UTF-8&amp;amp;amp;amp;amp;quot;));<br \/>\nsb.append(&amp;amp;amp;amp;amp;quot;&amp;amp;amp;amp;amp;amp;redirect_uri=&amp;amp;amp;amp;amp;quot;);<br \/>\nsb.append(URLEncoder.encode(callbackUrl, &amp;amp;amp;amp;amp;quot;UTF-8&amp;amp;amp;amp;amp;quot;));<br \/>\nsb.append(&amp;amp;amp;amp;amp;quot;&amp;amp;amp;amp;amp;amp;grant_type=&amp;amp;amp;amp;amp;quot;);<br \/>\nsb.append(URLEncoder.encode(&#8216;authorization_code&#8217;, &amp;amp;amp;amp;amp;quot;UTF-8&amp;amp;amp;amp;amp;quot;));<\/p>\n<p>String URL_TO_REQUEST_TOKEN= &#8216;https:\/\/accounts.google.com\/o\/oauth2\/token&#8217;<\/p>\n<p>URL url = new URL(URL_TO_REQUEST_TOKEN);<br \/>\nHttpURLConnection connection = (HttpURLConnection) url.openConnection();<br \/>\ntry {<br \/>\nconnection.setRequestMethod(&amp;amp;amp;amp;amp;quot;POST&amp;amp;amp;amp;amp;quot;);<br \/>\nconnection.setRequestProperty(&amp;amp;amp;amp;amp;quot;Content-Type&amp;amp;amp;amp;amp;quot;, &amp;amp;amp;amp;amp;quot;application\/x-www-form-urlencoded&amp;amp;amp;amp;amp;quot;);<br \/>\nconnection.setRequestProperty(&amp;amp;amp;amp;amp;quot;Content-Length&amp;amp;amp;amp;amp;quot;, &amp;amp;amp;amp;amp;quot;&amp;amp;amp;amp;amp;quot; + sb.toString().length());<br \/>\nconnection.setRequestProperty(&amp;amp;amp;amp;amp;quot;Host&amp;amp;amp;amp;amp;quot;, &amp;amp;amp;amp;amp;quot;accounts.google.com&amp;amp;amp;amp;amp;quot;);<br \/>\nOutputStreamWriter outputStreamWriter = new OutputStreamWriter(connection.getOutputStream());<br \/>\noutputStreamWriter.write(sb.toString());<br \/>\noutputStreamWriter.flush();<br \/>\nlog.debug(&amp;amp;amp;amp;amp;quot;Response code ${connection.responseCode} , Message : ${connection.responseMessage}&amp;amp;amp;amp;amp;quot;)<br \/>\nString resultData = connection.content.text<br \/>\ndef responseJson = JSON.parse(resultData)<br \/>\nString ACCESS_TOKEN = responseJson?.access_token<br \/>\n}<br \/>\ncatch (Exception e) {<br \/>\ne.printStackTrace()<br \/>\n}<br \/>\n}<br \/>\n[\/java]<\/p>\n<p><strong>3. Calling Google API.<\/strong><\/p>\n<p>Now, with the help of access token, we can call google API to fetch Data by appending the access token in the GET request.<\/p>\n<p>Example:<\/p>\n<p>To fetch person&#8217;s profile data<\/p>\n<p>[java]<br \/>\nGET :  https:\/\/www.googleapis.com\/oauth2\/v1\/userinfo?access_token=ACCESS_TOKEN<br \/>\n[\/java]<\/p>\n<p>To get list of profile acitivties<\/p>\n<p>[java]<br \/>\n GET :<br \/>\n[\/java]<\/p>\n<p><strong>References:-<\/strong><br \/>\nhttp:\/\/code.google.com\/apis\/accounts\/docs\/OAuth2WebServer.html<\/p>\n<p>http:\/\/code.google.com\/apis\/accounts\/docs\/OAuth2.html<\/p>\n<pre><\/pre>\n<p>Hope this helps..!!!<\/p>\n<p>Vishal Sahu<br \/>\nvishal[at]intelligrape[dot]com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my current project, i needed to integrate Google+ in the application using server-side API. Google uses OAuth2.0 protocol for authorization when our application tries to access the data. All we require is an access token to fetch data from Google using REST calls which serves data in JSON format. I implemented it using Web [&hellip;]<\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":6},"categories":[7],"tags":[398,701,702,700],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4687"}],"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\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=4687"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4687\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=4687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=4687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=4687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}