Joe Gregorio | 88243be | 2011-01-11 14:13:06 -0500 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # |
| 4 | # Copyright 2010 Google Inc. All Rights Reserved. |
| 5 | |
| 6 | """Simple command-line example for Google URL Shortener API. |
| 7 | |
| 8 | Command-line application that shortens a URL. |
| 9 | """ |
| 10 | |
| 11 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 12 | |
| 13 | from apiclient.discovery import build |
| 14 | |
| 15 | import pprint |
| 16 | |
| 17 | # Uncomment the next two lines to get very detailed logging |
| 18 | #import httplib2 |
| 19 | #httplib2.debuglevel = 4 |
| 20 | |
| 21 | |
| 22 | def main(): |
| 23 | |
| 24 | # Build the url shortener service |
| 25 | service = build("urlshortener", "v1", |
| 26 | developerKey="AIzaSyDRRpR3GS1F1_jKNNM9HCNd2wJQyPG3oN0") |
| 27 | url = service.url() |
| 28 | |
| 29 | # Create a shortened URL by inserting the URL into the url collection. |
| 30 | body = {"longUrl": "http://code.google.com/apis/urlshortener/" } |
| 31 | resp = url.insert(body=body).execute() |
| 32 | pprint.pprint(resp) |
| 33 | |
| 34 | shortUrl = resp['id'] |
| 35 | |
| 36 | # Convert the shortened URL back into a long URL |
| 37 | resp = url.get(shortUrl=shortUrl).execute() |
| 38 | pprint.pprint(resp) |
| 39 | |
| 40 | if __name__ == '__main__': |
| 41 | main() |