Skip to content

Instantly share code, notes, and snippets.

@joncasdam
Last active September 29, 2015 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joncasdam/ed430f560b99f2477ce5 to your computer and use it in GitHub Desktop.
Save joncasdam/ed430f560b99f2477ce5 to your computer and use it in GitHub Desktop.
import requests
import json
api_url = 'https://www.googleapis.com/urlshortener/v1/url'
api_key = '' # find here how to get yours https://developers.google.com/url-shortener/v1/getting_started#APIKey
url = '{0}?key={1}'.format(api_url, api_key)
headers = {'content-type': 'application/json'}
params = json.dumps({'longUrl': 'http://www.google.com'})
response = requests.post(url, data=params, headers=headers)
if response.ok:
print (response.json()['id'])
else:
print (response.status_code, response.reason)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment