Created
July 23, 2013 22:29
-
-
Save anonymous/6066736 to your computer and use it in GitHub Desktop.
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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")); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment