Pythonでbitly APIを使ってURLを短縮する例
# bitly APIのtokenと短縮前URL
def createShortURL(access_token, longUrl):
url = 'https://api-ssl.bitly.com/v3/shorten'
values = {
"access_token": access_token,
"longUrl": longUrl
}
response = urllib.request.urlopen(url, urllib.parse.urlencode(values).encode('utf-8'))
encoding = response.info().get_content_charset('utf-8')
data = json.loads(response.read().decode(encoding))
return data["data"]["url"]