Working With Gmail API

Hi friends today in this blog we will talk about the  Gmail REST API. On 25,June 2014 gmail launched it's API.This API is widely used among developers. 

We will implement this  API using python though documentation for gmail API in python is available but it is not updated. 

Let's begin , for using gmail API You need to enable gmail API for your account and create credentials for the API.

1. For Enabling gmail API visit Google Developer Console  and click on Enable APIS And Services.


2. Search for gmail API and then click on enable button.


                 

3.  Go to credentials option and then click on create credentials. 3 options will be displayed to you , select oauth client id.


4. Give name to your oauth app , if you are creating a django app using gmail API with python then you should add the link of your website also.

5. Install Python library for Gmail API by using following command.

 pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib

After all these steps your API is ready to use. So lets start its implementation.  

 Creating Usertoken

Before we start using gmail API functionalities we have to sign into our gmail account, so how to sign in ? . Let's begin

We will create a function called credentialcreater which will return a user token, this usertoken will help us to use the functionalities of gmail API.

So here is the code for credentialcreater


 So what basically we are doing in this script is , we are generating a pickle file called token.pickle, which contains the oauth verification details also if a file named token.pickle already exist then we will read the file and verify that token details are correct or not.

Before generating token we need the gmail API credentials for our script to run, so we will take input of the API credentials from the user which were generated in step 3 after that we will write these credentials to the file named client.json.

Let's try to understand what's actually  happening in credentialcreater , when user will call the function credentialcreater . 1st of it will check that the oauth credentials of the user are their or not i.e a file named token.pickle exist or not. If it exist then the token is valid or not. if it does not exist then it will check that the API credentials are present or not, If they are present then it will ask the user to open the link generated for oauth verification. In case if credentials doesn't excist then it will ask the user for API credentials.

After generating the usertoken we are ready to use the gmail API functionality. So let's begin. 

Gmail API Functonality

1. User Details :- Fetch details of the user from gmail API  like email address, Display Name etc.

2. Send Email With Attachment :-Send audio,video, pdf using gmail API

3. User Message Details :- Fetch user message details like total message count , threads etc.


You can do a lot with gmail API , read more about Gmail Api in its official documentation. For any queries or suggestions feel free to write in comment section.

 

 

Comments

Popular posts from this blog

How To Run Jupyter Notebook , Pandas And Numpy On M1 MacBook ?

M1 MacBook Air For Development (A Student Review)

Create A Simple Html Form With Google Sheets As Database