Template request | Bug report | Generate Data Product
Tags: #bitly #link #shorten #url #create #python
Author: Florent Ravenel
Description: This notebook will show how to create short links with Bitly.
References:
import requests
import json
token
: Generate a Bitly Access Tokenlong_url
: URL to be shortened
token = "<YOUR_TOKEN_HERE>"
long_url = "https://www.example.com/"
This function will use the Bitly API to shorten a given URL.
def shorten_url(token, long_url):
url = "https://api-ssl.bitly.com/v4/shorten"
headers = {"Authorization": "Bearer " + token}
payload = {"long_url": long_url}
response = requests.post(url, headers=headers, json=payload)
response_json = response.json()
return response_json["link"]
short_url = shorten_url(token, long_url)
print(short_url)