{"id":14199,"date":"2014-06-22T19:19:45","date_gmt":"2014-06-22T13:49:45","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=14199"},"modified":"2014-06-22T20:32:43","modified_gmt":"2014-06-22T15:02:43","slug":"oauth-2-0-using-grails-part-2","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/oauth-2-0-using-grails-part-2\/","title":{"rendered":"OAuth 2.0 using Grails Part \u2013 2"},"content":{"rendered":"<p><strong>Obtain accessToken from refreshToken<\/strong><\/p>\n<p>In the previous article <a title=\"OAuth 2.0 using Grails Part \u2013 1\" href=\"http:\/\/www.tothenew.com\/blog\/grails-way-of-oauth-2-0-to-access-google-apis-part-1\/\" target=\"_blank\">OAuth 2.0 using Grails Part \u2013 1<\/a> I have given a detailed explanation regarding user authentication using OAuth 2.0. In this article i will explain how you can obtain access_token from the refresh_token. Please refer to step(3) of part &#8211; 1 of this article where we obtained a JSON of the following format :<\/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>You need to persist the refresh_token in some database because access_token which we have got in the JSON is only valid for 1 hour. A fresh access_token can be obtained with the help of refresh_token. Following method will take persisted refresh_token as a parameter and will return a fresh access_token.<\/p>\n<p>[java]<br \/>\nString getLatestAccessToken(String refreshToken) {<br \/>\n String latestToken = &quot;&quot;<br \/>\n def http = new HTTPBuilder(&#8216;http:\/\/localhost:8080\/&#8217;)<br \/>\n http.request(POST) {<br \/>\n uri.path = &#8216;https:\/\/accounts.google.com\/o\/oauth2\/token&#8217;<br \/>\n requestContentType = &quot;application\/x-www-form-urlencoded&quot;<br \/>\n body = [<br \/>\nclient_id: &quot;{Client ID}&quot;,<br \/>\nclient_secret: &quot;{Client Secret}&quot;,<br \/>\n grant_type: &quot;refresh_token&quot;,<br \/>\n refresh_token: refreshToken<br \/>\n ]<br \/>\n response.success = { resp, json -&gt;<br \/>\n latestToken = json.access_token<br \/>\n }<br \/>\n }<br \/>\n return latestToken<br \/>\n }<br \/>\n[\/java]<\/p>\n<p>In the above code response.success closure contains parameter json which contains the response in the following format:<\/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;id_token&quot;: {id_token}<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>From this JSON we are obtaining access_token which is again valid for 1 hour only.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Obtain accessToken from refreshToken In the previous article OAuth 2.0 using Grails Part \u2013 1 I have given a detailed explanation regarding user authentication using OAuth 2.0. In this article i will explain how you can obtain access_token from the refresh_token. Please refer to step(3) of part &#8211; 1 of this article where we obtained [&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":1},"categories":[7],"tags":[1446,4840,9],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14199"}],"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=14199"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14199\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=14199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=14199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=14199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}