Although it is very simple, I haven’t found any example out there. This is a little JS class to call Google URL Shortener API from your widget.
It requires jQuery. Notice that it won’t work on your Web site, it is only for widgets.
/**
* @author Julio Rabadán (@ somms.net)
*/
var UrlShortener = function (_ApiKey)
{
this._apiKey = _ApiKey;
};
UrlShortener.prototype.getShortURL = function (_URL, _callback){
_URL = $.trim(_URL);
if(_URL){
var api_url = "https://www.googleapis.com/urlshortener/v1/url";
var data_POST = '{ "longUrl" : "' + _URL + '", "key" : "' + this._apiKey + '"}';
$.ajax({
url : api_url,
dataType : "json",
data : data_POST,
contentType : "application/json",
processData : false,
success : function(response){
_callback(response);
},
cache : true,
error : function(XMLHttpRequest, textStatus, errorThrown){
console.log(XMLHttpRequest, textStatus, errorThrown);
_callback({"status": "network_error"});
},
type : "POST"
});
}else{
return false;
}
}