Python and Gmail API

Now and then I run scraper and machine learning model on my aws. Sometime they take very long time to complete and I would love to get email when they complete or encounter some error.
They is easy way to to this using basic email module from python but in order to use this, you have to lower your security setting in Gmail i.e allow app with lower security. Plus you have to store your password in text format in script (or next file or path), which is not safe. 
One way around this is using google Gmail API. If you use google api you can revoke the access anytime you like and also don't need to lower the security setting nor need to store your password as plain text.

let get started:
1. Follow these step to  turn on sign into google developer console and get Gmail API.
2. Install following library

!pip install --upgrade google-api-python-client httplib2 oauth2client apiclient

3. We will make access token to use as password for future access using client secret json. You only need to authenticated the scrips once.  One created access token can be used to manage your gmail like a password.
This will open authentication window in your browser( if browser is no open you will see link in console, copy it and paste it into browser). It will ask you login and allow for permission to send email on your behalf, just click allow.
Authentication success message.

You should see new file in your input directory.
This access token will be use as your login password from now on. This is like your new password,  so keep it safe. If anyone get hold of this file they can easily mess with your account same way if they get your password ( this is a bit exaggeration, they can only send email as you have only allowed send mail but you get the point). Save it some where on safe directory as you only need file location for future use. 

4. Now that we have the access token lets write python scripts for sending message.

We can use function send_email or use decorator email_of_completion in order to send email. Advantage of using decorator is this that decorator will send email even if function fails with detail of error message.
You can combine access token and email of completion in single file and run access token only if access token path doesn't exist run access token but I prefer to keep them separate as I will use access_token.py in order to make other access token for various API.

Good practice:
1. You can put access token path into environment variable ( like ACCESS_TOKEN_GMAIL_API).
2. If you use linux server without GUI like aws than make access token in window or mac or linux with GUI as you need browser to authenticate the access, once access token is made just transfer access_token json to server.

Here is github link to file
python-utili -> search for access_token.py and email_of_completion.py.

Quotes from book I am reading:
"Reputation is like fine china, expensive to acquire, and easily broken. If you are not sure if something is right or wrong, consider whether you’d want it reported in the morning paper."

      --Alice Schroeder, The Snowball: Warren Buffett and the Business of Life


###################################Happy coding###############################

Unknown

I'm a Data-holic. I'm passionate about learning new things and playing with data. This blog is a place where I want to share my data analysis tips and tricks, data visualization and automation tips and tricks.

No comments:

Post a Comment