Archive for the ‘Google’ Category

Google URL Shortner API

Monday, January 24th, 2011
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 bit.ly, tinyurl earlier, when Google launched it’s own tool, i started using this more. Even though bit.ly 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 goo.gl
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 bit.ly etc. This can be applied to goo.gl 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
			<?php

				$myURL = 'http://www.dzone.com/links/index.html';
				$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, 'https://www.googleapis.com/urlshortener/v1/url');
				curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
				curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);

				$responseData = curl_exec($curl);

				$jsonData = json_decode($responseData);

				curl_close($curl);

				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;

			import java.io.BufferedInputStream;
			import java.io.PrintWriter;
			import java.net.URL;
			import javax.net.ssl.HttpsURLConnection;

			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\":\"" + "http://news.google.com.sg/news?cf=all&ned=in&hl=en&edchanged=1" + "\"}";
					try {
						simpleURL = new URL("https://www.googleapis.com/urlshortener/v1/url?key=yourAPIKey");
						url = (HttpsURLConnection)simpleURL.openConnection();
						url.setDoOutput(true);
						url.setRequestProperty("content-type", "application/json");
						PrintWriter pw = new PrintWriter(url.getOutputStream());
						pw.print(inputString);
						pw.close();
					}
					catch (Exception ex) {
					  return "Exception in Connecting to API";
					}

					  try {
						  bStream = new BufferedInputStream(url.getInputStream());
						  int i;
						  while ((i = bStream.read()) >= 0) {
							  resultString.append((char)i);
						  }
					  }
					  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
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Get Adobe Flash player