Google Shortener API For .NET Written In C#
Google Shortener is a service provided by Google to create short URLs. Google Shortener has been available for some time now but only recently Google released a public API to access this service programmatically. Today I decided to try it out and wrote some C# code to work with the API. I will describe what I did in this post.
A bit about the API
Like other URL shorteners, Google API also takes in a long URL and gives you back a short one. When you click on the short URL, you end up at the same location on web as you did with your original long URL.
The API itself is implemented as a RESTful service and is very simple. You can read the overview and reference for API here.
.NET Wrapper
I wanted to use the API in my .NET application so I created a wrapper. I have made the source code available at code.google.com. Feel free to download and play with it.
There are three methods in Google Shortener API:
- url.insert – Shrinks a Url.
- url.get – Returns the long Url for a valid short url.
- url.list – Returns a list of Urls shortened by an authenticated user. Analytics data is also returned by this method.
If you want analytics for your Url then you must invoke the methods by passing in an API key. You can get your API key from Google API Console.
Enough talk. Let me show you the code. The main class in the wrapper is called Shortener which contains two methods: GetShortUrl() and GetLongUrl(). An API key can be passed in to the constructor of Shortener and the API key will be used for all subsequent requests for that instance.
public Reply GetShortUrl(string longUrl)
{
string data = "{\"longUrl\":\"" + longUrl + "\"}";
string postUrl = googleShortenerUrl;
string response = HttpHelper.HttpPOST(postUrl, data);
return DeserializeJSON(response);
}
public Reply GetLongUrl(string shortUrl)
{
string getUrl = googleShortenerUrl + "&shortUrl=" + shortUrl;
string response = HttpHelper.HttpGET(getUrl);
return DeserializeJSON(response);
}
The response received from Google is deserialized into a Reply class.
public class Reply
{
public string kind { get; set; }
public string id { get; set; }
public string longUrl { get; set; }
public string status { get; set; }
}
GET and POST are done by using HttpHelper class. Download the code to see this class.
Conclusion
Google Shortener API is as simple as it gets. The API also let’s you retrieve analytics data for URLs. I have created a .NET wrapper for the API and have implemented functionality to get short URLs and long URLs. Next steps for this little project are to implement list method, implement authentication support and make the API a bit more rich by enhancing Reply class to support strongly typed members rather than all strings.
One final time. Link to code.
Top Posts
- LINQ To SQL Tutorial
- LINQ To SQL Join On Multiple Conditions
- Code Sample: Programmatically Download File Using C#
- Windows 7 Control Panel In Classic Mode
- More Details Emerge On Microsoft Master Certification
- Use SqlConnection With LINQ To SQL
- Capture XML In WCF Service
- Free Icons And Images With Visual Studio 2008
- Dynamic Sort With LINQ
- StyleCop Tutorial
Tags
.Net 2010 ADO.NET ASP.NET Azure Blogging Books Browsers C# Certification Cloud Computing Code Snippets Community Data Services Eclipse Entity Framework Google IDE Java LINQ Mac Microsoft Museum NetBeans Office Oracle REST SharePoint Silverlight SQL Server T-SQL Tips Tools Training Visual Studio Visual Studio 2010 WCF Web Windows Windows 7 Windows Forms Windows Live WMI WPF XAML

