Skip to content

Instantly share code, notes, and snippets.

@aezell
Created January 11, 2011 22:08
Show Gist options
  • Save aezell/775262 to your computer and use it in GitHub Desktop.
Save aezell/775262 to your computer and use it in GitHub Desktop.
Messing around with goo.gl URL shortener API
import json, urllib2, pprint
"""
Mess around with goo.gl shortener API
"""
kind = "SHORTEN"
key = "YOUR_KEY"
api_url = "https://www.googleapis.com/urlshortener/v1/url?key=%s" % key
long_url = "http://www.google.com/"
short_url = "http://goo.gl/fbsS"
if kind == "SHORTEN":
req = urllib2.Request(api_url, json.dumps({"longUrl": long_url}),
{'content-type': 'application/json'})
res = json.loads(urllib2.urlopen(req).read())
elif kind == "EXPAND":
api_url = api_url + "&shortUrl=%s" % short_url
res = json.loads(urllib2.urlopen(api_url).read())
elif kind == "STATS":
api_url = api_url + "&shortUrl=%s&projection=FULL" % short_url
res = json.loads(urllib2.urlopen(api_url).read())
pprint.pprint(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment