Categories
Coding

Python and the Gmail API

The other day an idea popped in my head for a side project that would involve the Gmail API. Whenever this happens, I normally hit up the developer site, find a simple tutorial, and immediately get my hands dirty.

A quick search led me to this page. It’s called a “Quickstart: Run a Gmail App in Python.” Perfect. I’ll run this script and be on my way in no time. Well, not so fast.

This tutorial assumes I’m doing development on my local machine where it can pop open a web browser and authorize the API access. Most of the time I don’t operate that way – I use a remote Linux server of some kind.

To get this quick start guide working on a remote server:

  1. In the Google Developers Console, generate a client ID for a native application (this may work with other types of client IDs, but I know it works with a native one)
  2. Save the client_secret JSON to the script directory
  3. Add the ability to read command line arguments and call the function ‘tools.run_flow()’ instead of just ‘run’
  4. Run the script with “–noauth_local_webserver” as an argument

Now when you run the script, you’ll get a link to copy into any browser to authorize the access. Then, you’ll get an auth code to paste back into your script.

I’ve included both scripts so that you can compare easily. I hope this saves others from frustration and I also hope Google improves the tutorial.

quickstart_original.py
quickstart.py