Friday, June 1, 2012

The Javascript Google URL Shortener Client API

I was working with the Google API Javascript Client this week to shorten the URLs of Google static maps generated by my ham radio QSL mapper. The client interface provided by Google is very useful. It took me a while to work through some of the less clear documentation, so I thought I'd add a few notes that would have helped me here. First, you only need to authenticate your application to the url shortener application if you want to track statistics on your shortened urls. If you just want the shortened URL, you don't need to worry about this. The worst part for me was that the smaple code only showed how to get a long url from an already shortened rul. If you follow the doucmentaiotn on the insert method, (the method for getting a shortened url from a long one), there is a reference to a rather nebulous Url resource required argument. It's not at all clear how to create one of these in Javascript. The following example code shows how:
  var request = gapi.client.urlshortener.url.insert({
      'resource': {
          'longUrl': 'http://copaseticflows.appspot.com'
      }
  });
  request.execute(function(response) {      
      if((response.id != null)){
       //do something with the shortened url contained in the response.id field
      }
  });
Picture of the Day
From 6/4/12

2 comments:

Anubhav said...

Thanks! This sample was exactly what i was looking for.

Blogger said...

Did you know you can create short urls with AdFly and get dollars for every visit to your short urls.