Open In App

URL Shorteners and its API in Python | Set-1

URL Shortener, as the name suggests, is a service to help to reduce the length of the URL so that it can be shared easily on platforms like Twitter, where number of characters is an issue. 
There are so many URL Shorteners available in the market today, that will definitely help you out to solve the purpose. We will be discussing API implementation of Bitly URL Shortener and implementation of a Python module pyshorteners. This is basically a library in Python that provides implementation of few popular URL Shorteners.
Bitly: Bitly provides a platform to shorten URL's, share them and keep a track of the activity on the shortened URL. Before starting using Bitly API, you first need to signup on the site to get an API Key. This is very important to get access to the API to use it for programming. 
 


 


Note: API keys are being deprecated, we recommend you use OAuth.
So we will be discussing Bitly API application using both API Keys and OAuth.
Before proceeding further let's first discuss what is an API Key. Application Programming Interface Key is a code that is passed by the computer calling an API that is used to identify the user, computer or we can say the calling program. This is basically used to control malicious activity in using an API. 
  
Bitly API Python Module Installation: 
 

  1. Following link shows the list of Bitly API code libraries that are available : Bitly API Code Libraries 
    In this post we will be using bitly-api-python library which is also official Python client.
  2. One way to install the python module is to use the pip command 
     
pip install bitly_api

  1.  
  2. In case the installation using pip command is showing error, uninstall bitly_api using the following command: 
     
pip uninstall bitly_api
  1. Download Bitly API Module using the following link bitly_api
  2. Unzip the downloaded folder and then navigate to the folder bitly-api-python-master, using the command: 
     
cd bitly-api-python-master
  1. Now install bitly_api module using the following command: 
     
python setup.py install
  1. On the command prompt type following set of commands to check whether module is successfully installed or not 
     
python
import bitly_api

  1. If no error shows, this means module is successfully installed. 
     

  1. Alternatively, you can execute test_bitly_api.py file using the command 
     
python test_bitly_api.py

  1. Absence of error means module is successfully installed.


Bitly API Implementation using API Key: 
 

import bitly_api 

API_USER = "username" 
API_KEY = "API_Key"
bitly = bitly_api.Connection(API_USER, API_KEY) 

response = bitly.shorten('http://google.com/') 

# Now let us print the Bitly URL 
print(response)

  
Bitly API Implementation using OAuth:
First we need to Generate OAuth token for the program. Note that only verified email id can be used to generate OAuth Token. 
 

import bitly_api

BITLY_ACCESS_TOKEN ="ACCESS_TOKEN" 

b = bitly_api.Connection(access_token = BITLY_ACCESS_TOKEN)

response = b.shorten('http://google.com/')
print(response) 

GFG_BITLY



 
Pyshorteners: Pyshorteners is a Python module that provides implementation for various URL Shortening services that are available in the market. 
Use pip command to install the module: 
 

pip install pyshorteners


  
Now let's discuss code implementation and output of Google URL shortening service.
Google URL Shortener: Before starting using Google URL Shortener we first need to signup, create a project and get an API Key for using the API. 
Steps to create an API Key are as follows: 
 


 

from pyshorteners import Shortener

long_url = 'http://www.google.com'

API_Key = 'AIzaSyBBS...jXKIGh1fNU'

url_shortener = Shortener('Google', api_key = API_Key)
print ("Short URL is {}".format(url_shortener.short(long_url)))

Output: 
 


Code to Expand the Shortened URL: 
 

from pyshorteners import Shortener

short_url ='https://goo.gl/fbsS'

API_Key = 'AIzaSyBBSL...jXKIGh1fNU'

url_expander = Shortener('Google', api_key = API_Key)

print ("Long URL is {}".format(url_expander.expand(short_url)))

Output: 
 


References: 
 


 

Article Tags :