用python生成goo.gl短网址

使用goo.gl的网址缩短API写了段代码。

# coding : utf-8
# date: 2013-2-23
import requests
import json

def url_shorten(long_url):

    query_params = {'key': 'AIzaSyCqkFbtu0hvFyweNEpROnBOyHvR6A6J8aY',
                        'longUrl': long_url}
    headers = {'content-type': 'application/json'}

    endpoint = 'https://www.googleapis.com/urlshortener/v1/url'
    response = requests.post(endpoint, data=json.dumps(query_params), headers=headers)
    data = json.loads(response.content)
    print "Long  URl: %s nShort URl: %s" %(long_url, data['id'])

if __name__== '__main__':
    url_shorten('http://www.chedanji.com')

参考资料:https://developers.google.com/url-shortener/v1/getting_started