Figuring out the Goo.gl API
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 < arguments.length; m++)
l = l + arguments[m] & 4294967295;
return l
}
function d(l) {
var m = String(l > 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 OpenSSL/0.9.8l zlib/1.2.3 > 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
Posted by Anant on December 15th, 2009 in Google, Hacks, Mozilla | 17 Comments
Dan remarks on December 15th, 2009 at 3:07 am
It might return the original URL if the “shortened” url would actually be longer. Try a longer url?
Matthew remarks on December 15th, 2009 at 4:11 am
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.
Fatalis remarks on December 16th, 2009 at 2:49 am
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
Lim Chee Aun remarks on December 16th, 2009 at 2:56 am
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
Fatalis remarks on December 16th, 2009 at 3:01 am
Oh ignore that, none of my code works properly, same as usual.
Fatalis remarks on December 16th, 2009 at 3:37 am
Woops, a bug in previous code. http://privatepaste.com/9e8a1c9591
Matthew remarks on December 16th, 2009 at 3:46 am
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.
Eugene remarks on December 16th, 2009 at 5:41 am
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.
» Figuring out the Goo.gl API h… Thej Live remarks on December 17th, 2009 at 6:34 pm
[...] out the Goo.gl API http://www.kix.in/blog/?p=575 you can use goo.gl outside the toolbar [...]
Matthew Flaschen remarks on December 17th, 2009 at 9:24 pm
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);
Marco remarks on December 19th, 2009 at 11:32 am
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
Matthew Flaschen remarks on December 21st, 2009 at 1:40 am
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.
takien remarks on December 29th, 2009 at 5:44 am
thanks for sharing, i wish i can add it to my short url code generator soon.. (check my link)
Figure out Google URL shortener – goo.gl « Android's Avatar remarks on January 9th, 2010 at 2:08 pm
[...] http://www.kix.in/blog/2009/12/goo-gl/ [...]
goo.gl URL Shortener Bookmarklet via YQL remarks on January 16th, 2010 at 5:41 pm
[...] service that will be used for Feedburner and the Google Toolbar. It wasn’t long before the URL shortening code in the toolbar was dissected, and subsequent APIs developed. I managed to put together my own by using [...]
ThomanPhan remarks on January 26th, 2010 at 10:21 pm
website
http://tinygl.com/
creat mass goo.gl link ease
Marcus Nunes remarks on February 10th, 2010 at 7:11 pm
Hey, I made a PHP version of goo.gl code.
Check this out:
http://marcusnunes.com/api-goo.gl.php