Google URL Shortener官方API

Google终于正式推出自家的URL Shortener http://goo.gl/供用户使用了。那么接口自然会是有的,不再需要像之前一样需要自己hack才能从外部调用goo.gl压缩网址。

API地址 http://goo.gl/api/shorten
参数 security_token 和 url
未登录就将security_token值设置未null

以下是Python代码写的示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env python
# encoding: utf-8
import urllib,httplib
def test():
url = 'goo.gl'
form_fields = {'security_token':'null',
'url':'http://google.com'}
params = urllib.urlencode(form_fields)
headers = {'Host':'goo.gl',
'User-Align':'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.10',
'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
'Connection':'Keep-Alive',
'Keep-Alive':115,
'X-Requested-With':'XMLHttpRequest',
'Referer':'http://goo.gl/',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
"Cookie":'authed=1'}
conn = httplib.HTTPConnection(url)
conn.request(method='POST',url = '/api/shorten',body=params,headers=headers)
response = conn.getresponse()
print response.status
 
res = response.read()
print res
 
getheaders = response.getheaders()
for head in getheaders:
print head[0],':',head[1]
conn.close()
 
if __name__ == '__main__':
test()

需要注意的是请求header信息里面必须携带Content-Type信息,否则会得到Bad request的400错误信息。
-EOF-