Tuesday, 23 July 2013

Quick and Easy Integration of Google URL Shortener API in your Java Applications using Scribe-Java and GSon

This post is about a quick and easy integration of Google’s URL Shortener Service/API in your Java Applications using popular APIs like Scribe-Java and Google’s GSon.

The good part about this integration is that you don’t need to do additional steps to register your application with the Service APIs, because in this case, the Google’s URL Shortener Service can be accessed “anonymously”, with out having the need to register the application and performing the authentication and authorization steps.

google url shortener - goo.gl.com

First Download:

Create a new project in IDE of your choice and add above downloaded JAR files to your project’s build/class path.

GoogleUrlShortenerApiIntegrationUsingScribeExample.java:

/**
* Quick and Easy Integration of Google URL Shortener API in your Java Applications using Scribe-Java and GSon
* http://codeoftheday.blogspot.com/2013/07/quick-and-easy-integration-of-google.html
*/
package smhumayun.codeoftheday.google.urlshortener;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import org.scribe.builder.ServiceBuilder;
import org.scribe.builder.api.GoogleApi;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Verb;
import org.scribe.oauth.OAuthService;
import java.lang.reflect.Type;
import java.util.Map;
/**
* This class demonstrate quick and Easy Integration of Google URL Shortener API in your Java Applications
* using Scribe-Java and GSon APIs
*
* User: smhumayun
* Date: 7/23/13
* Time: 5:27 PM
*/
public class GoogleUrlShortenerApiIntegrationUsingScribeAndGsonExample {
/**
* Main Method
*
* @param args arguments
*/
public static void main(String[] args) {
//Instantiating the oAuth Service of Scribe-Java API
OAuthService oAuthService = new ServiceBuilder()
//Google Api Provider - Google's URL Shortener API is part of Google Platform APIs
.provider(GoogleApi.class)
/*
Using "anonymous" as API Key & Secret because Google's URL Shortener service
does not necessarily requires App identification and/or User Information Access
*/
.apiKey("anonymous")
.apiSecret("anonymous")
//OAuth 2.0 scope for the Google URL Shortener API
.scope("https://www.googleapis.com/auth/urlshortener")
//build it!
.build();
//Instantiating oAuth Request of type POST and with Google URL Shortener API End Point URL
OAuthRequest oAuthRequest = new OAuthRequest(Verb.POST, "https://www.googleapis.com/urlshortener/v1/url");
//set the content type header to application/json - this is the type of content you are sending as payload
oAuthRequest.addHeader("Content-Type", "application/json");
//Preparing JSON payload to send url to Google URL Shortener
String json = "{\"longUrl\": \"http://h1b-work-visa-usa.blogspot.com/\"}";
//add xml payload to request
oAuthRequest.addPayload(json);
//send the request
Response response = oAuthRequest.send();
//print the response from server
System.out.println("response.getBody() = " + response.getBody());
//determining the generic type of map
Type typeOfMap = new TypeToken<Map<String, String>>() {}.getType();
//desrialize json to map
Map<String, String> responseMap = new GsonBuilder().create().fromJson(response.getBody(), typeOfMap);
//print id which is actually the shortened url
System.out.println("Shortened URL = " + responseMap.get("id"));
}
}

Compile and Run the project. You will notice an output similar to the one below in your IDE’s console:

# Quick and Easy Integration of Google URL Shortener API in your Java Applications using Scribe-Java and GSon
# http://codeoftheday.blogspot.com/2013/07/quick-and-easy-integration-of-google.html
response.getBody() = {
"kind": "urlshortener#url",
"id": "http://goo.gl/ePeF0j",
"longUrl": "http://h1b-work-visa-usa.blogspot.com/"
}
Shortened URL = http://goo.gl/ePeF0j

Copy the Shortened URL, open a browser of your choice and paste the Shortened URL into the browser’s Address Bar and press ENTER. You will notice how Google’s URL Shortener Service resolves your Shortened URL to your original (longer) URL.

Shortened URL:

google url shortener - shortened url

Original Longer URL resolved by Goo.gl:

google url shortener - original longer url

DOWNLOAD COMPLETE SOURCE FROM HERE

No comments:

Post a Comment