Skip to content

Instantly share code, notes, and snippets.

@AtsushiSakai
Last active December 3, 2022 06:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AtsushiSakai/4d29bc51194b280401907e85f73e888f to your computer and use it in GitHub Desktop.
Save AtsushiSakai/4d29bc51194b280401907e85f73e888f to your computer and use it in GitHub Desktop.
Get shorten url via bitly v4 API with Python
#
# Get access_token from: https://app.bitly.com/settings/api/
# Then, just call this function with your long url and access token
#
# Ref
# - https://dev.bitly.com/api-reference/#createBitlink
# - https://dev.bitly.com/docs/getting-started/introduction/
import requests
def get_shorten_url(long_url, access_token):
url = 'https://api-ssl.bitly.com/v4/shorten'
headers = {"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json"
}
body = {'long_url':long_url}
res = requests.post(url, headers=headers, json=body).json()
return res["link"] # shorten url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment