Integrating Tumblr with grails application

13 / Mar / 2014 by Vivek Garg 0 comments

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.

The steps involved in using Tumblr API in our grails application are as:-

#1.Register Application :

Register an application on Tumblr here and get an API/OAuth_Consumer key.
This process will generate application and provide API/OAuth_Consumer Key & Secret Key for getting access token for API calls.

#2.Get Access Token :

With the help of API key and API secret generated in the above step, we need to get access token for calls.
The Java wrapper which i used can be downloaded from here

Steps involved in getting access token.
#.A request token is sent to the server to get a URL for the user to give back a verification code.
#.The verification code and the request token are used in another request to get an access token.
Once we have that access token we can then use it to get data from the Tumblr.

Code to get verification_code is as :
[java]
def registerOnTumBlr() {

String apiKey = API_KEY obtained from registered Tumblr Application
String apiSecret = API_SECRET obtained from registered Tumblr Application
String callbackurl = http://www.example.com/myapp/tumBlrCallBack
// callback URL to get the access token

OAuthService service = new ServiceBuilder().provider(TumblrApi.class).apiKey(apiKey).apiSecret(apiSecret)
.callback(callbackurl).build()

Token requestToken = service.getRequestToken()
String authUrl = service.getAuthorizationUrl(token)

session[‘REQUEST_TOKEN’] = requestToken
// set Tumblr Request token in session, as we will be requiring it to get access token

redirect(url: authUrl)
}
[/java]

This will redirect user to Tumblr server to grant permissions for this application and provides an verification code to obtain access token.

As the user grants permission, the Tumblr will redirect it back to the application with verification code, which is used to retrieve access token.

Code to handle callback, where Tumblr sends verification code:

[java]

def tumBlrCallBack() {

Token requestToken =(Token)session[‘REQUEST_TOKEN’]
OAuthService service = new ServiceBuilder().provider(TumblrApi.class).apiKey(apiKey).apiSecret(apiSecret)
.callback(callbackurl).build()

Verifier verifier = new Verifier(request.getParameter("oauth_verifier"))
Token accessToken = service.getAccessToken(requestToken, verifier)
// required access token for API calls.

JumblrClient client = new JumblrClient(apiKey,apiSecret)
client.setToken(accessToken.getToken(), accessToken.getSecret())

com.tumblr.jumblr.types.User user = client.user()
//user object for accessing data.
}
[/java]

This worked for me.
Hope it helps.

Useful Links:


http://www.tumblr.com/docs/en/api/v2

https://github.com/tumblr/jumblr

Cheers!!!
Vivek Garg
vivek.garg@intelligrape.com
www.intelligrape.com

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *