def shorten_url(long_url):
bitly API endpoint for shortening URLs
api_url = https://apissl.bitly.com/v4/shorten
API key (replace with your own)
access_token = your_bitly_access_token
Headers for the request
headers = {
ContentType: application/json,
Authorization: fBearer {access_token}
}
Data to be sent in the request
data = {
long_url: long_url,
domain: bitly.com You can change this to another domain if needed
}
Make the POST request
response = requests.post(api_url, json=data, headers=headers)
Check if the request was successful
if response.status_code == 200:
result = response.json()
return result[link]
else:
print(Error:, response.status_code, response.text)
return None