Recent Comments

    Google URL Shortner API

    One of the most important Google Tool i use regularly is Google URL Shortner, this is very helpful when you work with Social networking sites especially, When we wanted to share an URL which is too long and can’t fit into the char limit, that time this tool becomes handy. Even though we had few URL Shortner utilities like, tinyurl earlier, when Google launched it’s own tool, i started using this more. Even though is also giving you the option to analyze your URL traffics and etc, I love to work with Google, since most of the time i’m logged into any of the Google services. So it’ll be easy to use
    Basically it takes the URL and convert into a small/tiny with very few characters. We can use tools like Unshort me to unshorten the URL which are shortern by etc. This can be applied to URL’s also. As like all other services, Google provides API for this also. The API details can be found here. As like Google’s other API’s we need to have the Key to use this API as well.
    To get your API activated on your profile, Please visit here. In the left hand panel, you can get your Key created for your profile and you can use it on your code. You can use programming languages of your choice and get the JSON data parsed which is returned from Google API Service.
    We’ll look at two examples, of how to make a call to URL Shortner API. One with PHP and other one with Java.
    Using PHP to Call API Service
    				$myURL = '';
    				$googleAPIKey = 'URAPIKey';
    				$requestData = array('longUrl' => $myURL, 'key' => $googleAPIKey);
    				$jsonDetails = json_encode($requestData);
    				$curl = curl_init();
    				curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
    				curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonDetails);
    				curl_setopt($curl, CURLOPT_URL, '');
    				curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    				curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    				$responseData = curl_exec($curl);
    				$jsonData = json_decode($responseData);
    				echo 'Shortened URL is '.$jsonData->id;
    Code in Action,Click Here
    Using Java to Call API Service
    I have used the basic “HttpsURLConnection”, but still you have plenty of other options to call Google’s API using Java.
    			package com.techmaddy;
    			public class URLShortnerDemo {
    				public static void main(String[] args) {
    					System.out.println("The URL is "+getShortURL());
    				} //End of main
    				public static String getShortURL() {
    					URL simpleURL = null;
    					HttpsURLConnection url = null;
    					BufferedInputStream bStream = null;
    					StringBuffer resultString = new StringBuffer("");
    					String inputString = "{\"longUrl\":\"" + "" + "\"}";
    					try {
    						simpleURL = new URL("");
    						url = (HttpsURLConnection)simpleURL.openConnection();
    						url.setRequestProperty("content-type", "application/json");
    						PrintWriter pw = new PrintWriter(url.getOutputStream());
    					catch (Exception ex) {
    					  return "Exception in Connecting to API";
    					  try {
    						  bStream = new BufferedInputStream(url.getInputStream());
    						  int i;
    						  while ((i = >= 0) {
    					  catch (Exception ex) {
    							return "Exception in Reading Result";
    					return resultString.toString();
    				} //End of getShortURL
    			} //End of URLShortnerDemo
    Output for JAVA
    Google URL Shortner API Java OP

    Leave a Reply




    You can use these HTML tags

    <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>