{"id":12286,"date":"2014-03-13T14:30:03","date_gmt":"2014-03-13T09:00:03","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=12286"},"modified":"2016-12-19T15:16:42","modified_gmt":"2016-12-19T09:46:42","slug":"integrating-tumblr-with-grails-application","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/integrating-tumblr-with-grails-application\/","title":{"rendered":"Integrating Tumblr with grails application"},"content":{"rendered":"<p>In my current grails project, i needed to integrate Tumblr with the application using Tumblr API. I searched a lot about it and find the Java wrapper of Tumblr to use it with my grails application and thought it worth sharing.<br \/>\nTumblr uses OAuth1.0 protocol for authorization when our application tries to access the data.<\/p>\n<p>The steps involved in using Tumblr API in our grails application are as:-<\/p>\n<p>#1.<strong>Register Application :<\/strong><\/p>\n<p>Register an application on Tumblr <a href=\"http:\/\/www.tumblr.com\/oauth\/apps\" title=\"here\" target=\"_blank\">here<\/a> and get an API\/OAuth_Consumer key.<br \/>\nThis process will generate application and provide <strong>API\/OAuth_Consumer Key<\/strong> &amp; <strong>Secret Key<\/strong> for getting access token for API calls.<\/p>\n<p>#2.<strong>Get Access Token :<\/strong><\/p>\n<p>With the help of API key and API secret generated in the above step, we need to get access token for calls.<br \/>\nThe Java wrapper which i used can be downloaded from <a href=\"http:\/\/mvnrepository.com\/artifact\/com.tumblr\/jumblr\/0.0.6\" title=\"here\" target=\"_blank\">here<\/a><\/p>\n<p>Steps involved in getting access token.<br \/>\n<strong>#.<\/strong>A request token is sent to the server to get a URL for the user to give back a verification code.<br \/>\n<strong>#.<\/strong>The verification code and the request token are used in another request to get an access token.<br \/>\nOnce we have that access token we can then use it to get data from the Tumblr.<\/p>\n<p>Code to get verification_code is as :<br \/>\n[java]<br \/>\ndef registerOnTumBlr() {<\/p>\n<p>String apiKey = API_KEY obtained from registered Tumblr Application<br \/>\nString apiSecret = API_SECRET obtained from registered Tumblr Application<br \/>\nString callbackurl = http:\/\/www.example.com\/myapp\/tumBlrCallBack<br \/>\n\/\/ callback URL to get the access token<\/p>\n<p>OAuthService service = new ServiceBuilder().provider(TumblrApi.class).apiKey(apiKey).apiSecret(apiSecret)<br \/>\n.callback(callbackurl).build()<\/p>\n<p>Token requestToken = service.getRequestToken()<br \/>\nString authUrl = service.getAuthorizationUrl(token)<\/p>\n<p>session[&#8216;REQUEST_TOKEN&#8217;] = requestToken<br \/>\n\/\/ set Tumblr Request token in session, as we will be requiring it to get access token<\/p>\n<p>redirect(url: authUrl)<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>This will redirect user to Tumblr server to grant permissions for this application and provides an verification code to obtain access token.<\/p>\n<p>As the user grants permission, the Tumblr will redirect it back to the application with verification code, which is used to retrieve access token.<\/p>\n<p>Code to handle callback, where Tumblr sends verification code:<\/p>\n<p>[java]<\/p>\n<p>def tumBlrCallBack() {<\/p>\n<p>Token requestToken =(Token)session[&#8216;REQUEST_TOKEN&#8217;]<br \/>\nOAuthService service = new ServiceBuilder().provider(TumblrApi.class).apiKey(apiKey).apiSecret(apiSecret)<br \/>\n.callback(callbackurl).build()<\/p>\n<p>Verifier verifier = new Verifier(request.getParameter(&quot;oauth_verifier&quot;))<br \/>\nToken accessToken = service.getAccessToken(requestToken, verifier)<br \/>\n\/\/ required access token for API calls.<\/p>\n<p>JumblrClient client = new JumblrClient(apiKey,apiSecret)<br \/>\nclient.setToken(accessToken.getToken(), accessToken.getSecret())<\/p>\n<p>com.tumblr.jumblr.types.User user = client.user()<br \/>\n\/\/user object for accessing data.<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>This worked for me.<br \/>\nHope it helps.<\/p>\n<p>Useful Links:<\/p>\n<p><a href=\"http:\/\/www.tumblr.com\/docs\/en\/api\/v2\" title=\"http:\/\/www.tumblr.com\/docs\/en\/api\/v2\" target=\"_blank\"><br \/>\nhttp:\/\/www.tumblr.com\/docs\/en\/api\/v2<\/a><br \/>\n<a href=\"https:\/\/github.com\/tumblr\/jumblr\" title=\"https:\/\/github.com\/tumblr\/jumblr\" target=\"_blank\">https:\/\/github.com\/tumblr\/jumblr<\/a><\/p>\n<p>Cheers!!!<br \/>\nVivek Garg<br \/>\nvivek.garg@intelligrape.com<br \/>\n<a href=\"http:\/\/www.tothenew.com\" title=\"www.intelligrape.com\" target=\"_blank\">www.intelligrape.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my current grails project, i needed to integrate Tumblr with the application using Tumblr API. I searched a lot about it and find the Java wrapper of Tumblr to use it with my grails application and thought it worth sharing. Tumblr uses OAuth1.0 protocol for authorization when our application tries to access the data. [&hellip;]<\/p>\n","protected":false},"author":69,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[7],"tags":[770,1352,700,882,1351,1350],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/12286"}],"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\/69"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=12286"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/12286\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=12286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=12286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=12286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}