Last active
December 3, 2022 06:08
-
-
Save AtsushiSakai/4d29bc51194b280401907e85f73e888f to your computer and use it in GitHub Desktop.
Get shorten url via bitly v4 API with Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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