Google URL Shortening

Google has a new URL shortening API. This API works much like bit.ly and others, with one notable exception: it has an API!

Like most Google API’s this one is REST based so you can access it in any language rather easily. For this tutorial I will demonstrate this in C#.

Why use the Google for URL Shortening?

Google is growing rapidly and I think it’s fair to say they will be around for quite a long time. Whatever service you choose to shorten your URL’s is up to you, but I would suggest something with longevity. And having a clever moniker such as goo.gl is nice too!

Getting to know the Google Url Shortner API

To use the API you do not require an API key, but by using an API key you get increased limits to the amount of times you can call the API and you get statistics through Google analytics.

The API can do the following:

* Can shorten URL’s
* Can take a shortened URL and expand it back to the long URL
* Can give you Analytics info
* Can give you user history

This article will only cover the first 2 of those, but the code shown here could easily be applied to cover the last two if you like.

Making a Short URL with Google

To shorten a long URL you need to do an HTTP POST with a JSON payload that includes the long URL. Google will then give you back a JSON object that has the short URL in it.

Getting the Long Url from the Short

To lookup the long url you need to send an HTTP GET with the short url as a parameter, and then Google Shortner API will return a JSON reply with the long URL.

C# Google Shortner Classes

This is the main class that send the responses and gets the replies from the Google Shortner API.

GoogleUrlShortner.cs
?
using System.IO;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;

namespace Jarloo
{
public class GoogleUrlShortner
{
public static GoogleUrlReply Shorten(string longUrl, string apiKey=”")
{
string data = “{\”longUrl\”:\”" + longUrl + “\”}”;
string gUrl = “https://www.googleapis.com/urlshortener/v1/url”;

if (!string.IsNullOrEmpty(apiKey)) gUrl += “?key=” + apiKey;

string response = Post(gUrl, data);
return FromJSON(response);
}

public static GoogleUrlReply Expand(string shortUrl, string apiKey=”")
{
string gUrl = “https://www.googleapis.com/urlshortener/v1/url?shortUrl=” + shortUrl;

if (!string.IsNullOrEmpty(apiKey)) gUrl += “&key=” + apiKey;

string response = Get(gUrl);
return FromJSON(response);
}

public static string Get(string url)
{
using (WebClient web = new WebClient())
{
return web.DownloadString(url);
}
}

private static string Post(string url, string data)
{
WebRequest request = WebRequest.Create(url);
request.Method = “POST”;
request.ContentType = “application/json”;

byte[] byteData = Encoding.UTF8.GetBytes(data);

request.ContentLength = byteData.Length;

using (Stream s = request.GetRequestStream())
{
s.Write(byteData, 0, byteData.Length);
s.Close();
}

string replyData;

using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
{
using (Stream dataStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(dataStream))
{
replyData = reader.ReadToEnd();
}
}
}

return replyData;
}

private static T FromJSON(string input)
{
MemoryStream stream = new MemoryStream();

try
{
DataContractJsonSerializer jsSerializer = new DataContractJsonSerializer(typeof (T));
stream = new MemoryStream(Encoding.UTF8.GetBytes(input));
T obj = (T) jsSerializer.ReadObject(stream);

return obj;
}
finally
{
stream.Close();
stream.Dispose();
}
}
}
}

The GoogleUrlReply class is the deserialized representation of Google’s reply.

GoogleUrlReply.cs
?
namespace Jarloo
{
public class GoogleUrlReply
{
public string kind { get; set; }

//is the short URL
public string id { get; set; }

//is the long URL to which it expands.
//longUrl may not be present in the response, for example, if status is “REMOVED”.
public string longUrl { get; set; }

//is “OK” for most URLs.
//If Google believes that the URL is fishy,
//status may be something else, such as “MALWARE”.
public string status { get; set; }
}
}

read more

Try it

http://goo.gl/

Both comments and pings are currently closed.

24 Responses to “Google URL Shortening”

  1. live twitter says:

    Twitter is amazingly useful for obtaining busting information and all the latest news swiftly.

  2. Alexander7 says:

    buy@generic.LEVITRA” rel=”nofollow”>…

    Need cheap generic LEVITRA?…

  3. GPlus Widget says:

    Can’t wait for the Google+ API to come out.. Looking to develop my own GPLUS App

  4. The modern Zune browser might be astonishingly fantastic, except for as well as typically the iPod’s. It again successful, and yet isn’t actually for the reason that rapid for the reason that Safari, not to mention carries a clunkier user interface. Any time you in some cases consider together with the browser it is easy, and yet for anyone going to browse the web site at present . from a PMP then that iPod’s more substantial panel not to mention healthier browser could possibly be fundamental.

  5. Erin Yezek says:

    You present a few helpful ideas! Perhaps I should think of trying to do this myself.

  6. SALVADOR says:

    Purchase@Abilify.Without.Prescription” rel=”nofollow”>..

    Buygeneric meds…

  7. JEFFREY says:

    Order@Cheap.Acai” rel=”nofollow”>…

    Buynow it…

  8. TERRENCE says:

    Cheap@Generic.Acai” rel=”nofollow”>..

    Buyit now…

  9. DONNIE says:

    Buy@Discount.Coral.Calcium” rel=”nofollow”>..

    Buywithout prescription…

  10. ENRIQUE says:

    Purchase@Cheap.Coral.Calcium” rel=”nofollow”>.

    Buynow it…

  11. Lovely just what I was looking for.Thanks to the author for taking his time on this one. vps hosting | best vps host |

  12. ROY says:

    Purchase@Cheap.Coral.Calcium” rel=”nofollow”>…

    Buywithout prescription…

  13. EDUARDO says:

    Purchase@Abilify.Without.Prescription” rel=”nofollow”>..

    Buygeneric pills…

  14. WILLIE says:

    Buy@Discount.Acai” rel=”nofollow”>…

    Buygeneric drugs…

  15. Allan says:

    This web site is really a walk-through for all of the info you wanted about this and didn’t know who to ask. Glimpse here, and you’ll definitely discover it.

  16. CHARLIE says:

    Order@Cheap.Acai” rel=”nofollow”>..

    Buynow it…

  17. WALLACE says:

    Get@Acai.Online” rel=”nofollow”>…

    Buygeneric drugs…

  18. VICTOR says:

    Buy@Acai.Without.Prescription” rel=”nofollow”>.

    Buygeneric drugs…

  19. TONY says:

    Buy@Discount.Energy.Boost” rel=”nofollow”>.…

    Buynow it…

  20. BILLY says:

    Cheap@Accupril.Online” rel=”nofollow”>.…

    Buygeneric drugs…

  21. SAM says:

    Buy@Cheap.Accutane” rel=”nofollow”>.…

    Buygeneric drugs…

  22. CARL says:

    Cheap@Accutane.Online” rel=”nofollow”>.…

    Buygeneric meds…

  23. ADAM says:

    Order@Aciphex.Online” rel=”nofollow”>.…

    Buyno prescription…

  24. Google URL Shortening | Nakul Anand Wonderful goods from you, man. I’ve understand your stuff previous to and you’re just too excellent. I actually like what you’ve acquired here, really like what you’re saying and the way in which you say it. You make it entertaining and you still care for to keep it sensible. I cant wait to read far more Google URL Shortening | Nakul Anand again from you. Thanks For Share .

Powered by WordPress | Designed by: Free Web Space | Thanks to Highest CD Rates, Boat Insurance and UK Fiverr