UPDATE: ‘Fatalis’ has pointed out in the comments that the POST should be made to http://goo.gl/api/url with User-agent set to ‘toolbar’. The code now works, Yay!
Google just announced their own URL shortening service. Their service can only be used from the toolbar or FeedBurner, and I don’t particularly like adding extra toolbars to my browser. Maybe I can figure out a way to use their service from the command line?
I downloaded the toolbar XPI, unzipped it and peeked inside. Horribly indented JS awaited me. Nothing jsbeautifier couldn’t fix though. Few minutes later, I arrived at this readable JS function:
var getUrlShorteningRequestParams = function (b) {
function c() {
for (var l = 0, m = 0; m 0 ? l : l + 4294967296);
for (var o = 0, n = false, p = m.length - 1; p >= 0; --p) {
var q = Number(m.charAt(p));
if (n) {
q *= 2;
o += Math.floor(q / 10) + q % 10
} else o += q;
n = !n
}
m = m = o % 10;
o = 0;
if (m != 0) {
o = 10 - m;
if (l.length % 2 == 1) {
if (o % 2 == 1) o += 9;
o /= 2
}
}
m = String(o);
m += l;
return l = m
}
function e(l) {
for (var m = 5381, o = 0; o < l.length; o++)
m = c(m << 5, m, l.charCodeAt(o));
return m
}
function f(l) {
for (var m = 0, o = 0; o < l.length; o++)
m = c(l.charCodeAt(o), m << 6, m << 16, -m);
return m
}
var i = e(b);
i = i >> 2 & 1073741823;
i = i >> 4 & 67108800 | i & 63;
i = i >> 4 & 4193280 | i & 1023;
i = i >> 4 & 245760 | i & 16383;
var h = f(b);
var k = (i >> 2 & 15) << 4 | h & 15;
k |= (i >> 6 & 15) << 12 | (h >> 8 & 15) << 8;
k |= (i >> 10 & 15) << 20 | (h >> 16 & 15) << 16;
k |= (i >> 14 & 15) << 28 | (h >> 24 & 15) << 24;
j = "7" + d(k);
i = "user=toolbar@google.com&url=";
i += encodeURIComponent(b);
i += "&auth_token=";
i += j;
return i
};
So, I call getUrlShorteningRequestParams("http://www.kix.in/"); to get "user=toolbar@google.com&url=http%3A%2F%2Fwww.kix.in%2F&auth_token=78925814685". I see in their code that they do a POST request to the service to obtain a JSON return value that would contain the short URL. I punch it in using cURL:
$ curl -v -d\ "user=toolbar@google.com&url=http%3A%2F%2Fwww.kix.in%2F&;\ auth_token=78925814685" http://goo.gl/ * About to connect() to goo.gl port 80 (#0) * Trying 74.125.19.102... connected * Connected to goo.gl (74.125.19.102) port 80 (#0) > POST / HTTP/1.1 > User-Agent: curl/7.19.7 (i386-apple-darwin10.2.0) libcurl/7.19.7 > Host: goo.gl > Accept: */* > Content-Length: 77 > Content-Type: application/x-www-form-urlencoded > < HTTP/1.1 405 HTTP method POST is not supported by this URL
Oops! Well, not really, the URL shortener from the toolbar doesn’t work either, I just get the full URL whenever I try to “share” something. Has anybody actually generated a real goo.gl short URL yet?
Their auth_token parameter seems completely superfluous to me as it is generated from the URL itself. Don’t we all know security by obscurity doesn’t work
It might return the original URL if the “shortened” url would actually be longer. Try a longer url?
Dan, that’s a good guess. However, it doesn’t check out. No matter what the URL (and no matter whether you use curl, wget, or the toolbar) you get the 405 POST not allowed response and thus no short URL.
1. You’re posting to the wrong url. http://goo.gl/api/url
2. Use “toolbar” as User-Agent
3. They check for Cache-Control. It must be “no-cache”
Here’s my C# code: http://privatepaste.com/8ba361958b
Hi, someone else manage to make it work: http://alexandre.gaigalas.net/blog/2009/12/goo-gl-encurte-urls-com-o-novo-servico-do-google/
And I manage to do so as well: http://jsbin.com/idalu3
Oh ignore that, none of my code works properly, same as usual.
Woops, a bug in previous code. http://privatepaste.com/9e8a1c9591
Just wanted to know I submitted a bug email, and they replied and acknowledged the problem. They say they’re going to put out a new version of the toolbar, which means they’ve elected to change the client code rather than (or in addition to) the server. It will be interesting to see if they delve further into the security through obscurity realm. Alternatively, they may just bow to the inevitable and release a simple public API.
This is what you need to do:
curl -A “toolbar” -v -d “&user=toolbar@google.com&url=http%3a%2f%2fwww.bing.com%2fsearch%3fq%3dtest%26FORM%3dMSNH11%26qs%3dn&auth_token=71875998484″ http://goo.gl/api/url
However, the auth_token that gets generated by the javascript in toolbar.js is wrong. I think they released it with a bug. Have tried on mac and on windows.
Pingback: » Figuring out the Goo.gl API h… Thej Live
One of your simplifications to the (deliberately) unreadable function breaks it:
d(l) in the original starts with:
function d(l){l=l=String(l>0?l:l+4294967296);var m;m=l;
Beautified this is:
function d(l) {
l = l = String(l > 0 ? l : l + 4294967296);
var m;
m = l;
You have above:
function d(l) {
var m = String(l > 0 ? l : l + 4294967296);
You never reset l, but it is used later in d. This took me a while to figure out, because it only matters on certain URLs, and sometimes only slightly changes the token (but close only counts in horse-shoes and hand-grenades). I recommend:
function d(l) {
var m;
m = l = String(l > 0 ? l : l + 4294967296);
Here, y0u can create a short link of Goo.gl online:
http://geekgen.it/google-url-shortener
is gratis, and have API, for your site
I have also released an API. It supports JSONP, so you can use it directly from client-side scripts (and JSON, for elsewhere). This used by my Firefox extension (https://addons.mozilla.org/en-US/firefox/addon/55308) and my bookmarklet (http://www.marklets.com/Bookmarklets/goo.gl bookmarklet.aspx).
See http://ggl-shortener.appspot.com/instructions for details.
thanks for sharing, i wish i can add it to my short url code generator soon.. (check my link)
Pingback: Figure out Google URL shortener – goo.gl « Android's Avatar
Pingback: goo.gl URL Shortener Bookmarklet via YQL
website
http://tinygl.com/
creat mass goo.gl link ease
Hey, I made a PHP version of goo.gl code.
Check this out:
http://marcusnunes.com/api-goo.gl.php
http://eleven-twelve.net/python/googl-api.html
Pingback: Como integrar Goo.gl en tu web en 5 minutos | Timersys - Tutoriales, recursos web, JQuery y PHP, Wordpress , twitter y muchos recursos más
Pingback: NewsGrange » Google’s Superfast URL Shortener Gets an API: Coming to an App Near You Soon