{"id":13634,"date":"2014-05-13T14:20:50","date_gmt":"2014-05-13T08:50:50","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=13634"},"modified":"2016-12-19T15:07:33","modified_gmt":"2016-12-19T09:37:33","slug":"grails-way-of-oauth-2-0-to-access-google-apis-part-1","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/grails-way-of-oauth-2-0-to-access-google-apis-part-1\/","title":{"rendered":"OAuth 2.0 using Grails Part &#8211; 1"},"content":{"rendered":"<p>Following steps need to be followed for OAuth 2.0 authentication :<\/p>\n<h3>(1) Obtain OAuth 2.0 credentials from the Google Developers Console:<\/h3>\n<p><\/br><br \/>\nFirst you need to have a google account. create a project at <a href=\"https:\/\/console.developers.google.com\/project\" target=\"_blank\">Google Developers Console<\/a> after logging into your Google account. Click on the project name go to &#8220;APIs and auth&#8221; click on credentianls and then create a client ID. You will get client id and client secret which is required for OAuth 2.0 authentication . You also need to add Javascript origin and redirect URI&#8217;s by clicking on edit settings.<\/p>\n<p>Let us assume javascript origin is &#8220;http:\/\/localhost:8080\/&#8221;<br \/>\nand redirect URI is &#8220;http:\/\/localhost:8080\/TestOAuth\/google\/success&#8221;<br \/>\n<\/br><\/p>\n<h3>(2) Get authorization code.<\/h3>\n<p><\/br><br \/>\nFirst you need to obtain authorization code in order to get the access token.<br \/>\nYou need to have a link where a user can click to initiate the process of OAuth 2.0<br \/>\nThe link should be as follows:<\/p>\n<p>[html]<\/p>\n<p>&lt;a href=&quot;https:\/\/accounts.google.com\/o\/oauth2\/auth?redirect_uri=http%3A%2F%2Flocalhost:8080%2FTestOAuth%2Fgoogle%2Fsuccess<br \/>\n &amp;<br \/>\nresponse_type=code<br \/>\n&amp;<br \/>\nclient_id={your client id}<br \/>\n&amp;<br \/>\nscope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile<br \/>\n&amp;<br \/>\napproval_prompt=force<br \/>\n&amp;<br \/>\naccess_type=offline&quot;&gt; SignInWithGoogle &lt;\/a&gt;<\/p>\n<p>[\/html]<\/p>\n<p>After clicking on the above link you need to login using google account . After logging in you will be redirected to &#8220;http:\/\/localhost:8080\/TestOAuth\/google\/success?code={authorization code}&#8221;<\/p>\n<p>You can get the authorization code token from Querystring<br \/>\n<\/br><\/p>\n<h3>(3) Exchange authorization code for access token<\/h3>\n<p><\/br><br \/>\nPlace the following line in BuildConfig.groovy<\/p>\n<p>[java]<br \/>\nplugins {<br \/>\nruntime &quot;:rest:0.7&quot;<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>This plugin is required to make Get and POST Requests<\/p>\n<p>In the GoogleController.groovy we need to have a success action as follows:<\/p>\n<p>[java]<br \/>\ndef success(String code) {<br \/>\n        String googleaccesstoken = &quot;&quot;<br \/>\n        JSONObject googleJsonResponseForAccessToken<br \/>\n        def http = new HTTPBuilder(&#8216;http:\/\/localhost:8080\/&#8217;)<br \/>\n        http.request(POST) {<br \/>\n            uri.path = &quot;https:\/\/accounts.google.com\/o\/oauth2\/token&quot;<br \/>\n            requestContentType = &quot;application\/x-www-form-urlencoded&quot;<br \/>\n            body = [code: &quot;${code}&quot;,<br \/>\n                    client_id: &quot;{Client ID}&quot;,<br \/>\n                    client_secret: &quot;{Client Secret}&quot;,<br \/>\n                    redirect_uri: &quot;http:\/\/localhost:8080\/TestOAuth\/google\/success&quot;,<br \/>\n                    grant_type: &quot;authorization_code&quot;]<\/p>\n<p>            response.success = { resp, json -&gt;<br \/>\n                println &quot;POST response status: ${resp.statusLine}&quot;<br \/>\n                googleJsonResponseForAccessToken = json<br \/>\n                googleaccesstoken = json.access_token<br \/>\n            }<br \/>\n        }<\/p>\n<p>}<\/p>\n<p>[\/java]<\/p>\n<p>In the above code we are making a POST request to  and in response we will get following json<\/p>\n<p>[java]<br \/>\n{<br \/>\n&quot;access_token&quot;: {access_token},<br \/>\n&quot;token_type&quot;: &quot;Bearer&quot;,<br \/>\n&quot;expires_in&quot;: 3600,<br \/>\n&quot;refresh_token&quot;: {refresh_token},<br \/>\n&quot;id_token&quot;: {id_token}<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>This response is stored in json object of response.success method in the above code . we have stored the value of access_token in variable &#8220;googleaccesstoken&#8221; in the code above.<br \/>\n<\/br><\/p>\n<h3>(4) Get UserInfo from access_token<\/h3>\n<p><\/br><br \/>\nTo get the information of the user you need to make following request:<\/p>\n<p>[java]<br \/>\nJSONObject userInfo<br \/>\nhttp.request(GET) {<br \/>\n            uri.path = &quot;https:\/\/www.googleapis.com\/userinfo\/v2\/me&quot;<br \/>\n            uri.query = [accesstoken:googleaccesstoken]<br \/>\n            headers.&#8217;Authorization&#8217;=&quot;Bearer ${googleaccesstoken}&quot;<br \/>\n            response.success = { resp, json -&gt;<br \/>\n                println &quot;Get response status: ${resp.statusLine}&quot;<br \/>\n                userInfo=json<br \/>\n            }<br \/>\n        }<br \/>\n       render &quot;User Name  ::  &quot;+userInfo.name<br \/>\n       render &quot;User Id  ::  &quot;+userInfo.id<\/p>\n<p>[\/java]<\/p>\n<p>In the above code we have used the access_token which we got from the json response in step (3)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Following steps need to be followed for OAuth 2.0 authentication : (1) Obtain OAuth 2.0 credentials from the Google Developers Console: First you need to have a google account. create a project at Google Developers Console after logging into your Google account. Click on the project name go to &#8220;APIs and auth&#8221; click on credentianls [&hellip;]<\/p>\n","protected":false},"author":102,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":9},"categories":[7],"tags":[4840,9,1160],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/13634"}],"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\/102"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=13634"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/13634\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=13634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=13634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=13634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}