I get a lot of emails from a certain financial institution and I decided I wanted to parse the information they send me as best I could. This is actually pretty easy when you’ve got a Gmail account, because you can hook into your email in a non-clunky way.
conda install -c mutirri google-api-python-client
But that didn’t do what I expected, so I launched the Anaconda Prompt and did as Google said and ran the typical:
pip install --upgrade google-api-python-client oauth2client
When I ran that, I got an error saying that
twisted 18.7.0 requires PyHamcrest>=1.9.0, which is not installed.
So, I ran…
pip install PyHamcrest
You will also have to install argparse if you do not already have it. There may be other dependencies to download if not using the default installation of Anaconda.
To securely access your own account from the API, you must direct your script to your credentials. This is easily done by creating a project on your account and downloading the credentials as a .json file. From this link:
- Sign into your Google account if you are not already
- Click “Enable the Gmail API”
- Create a project by selecting “Create a new project” from the “Select or create project” dropdown in the dialog
- Enter a project name and click next
- Click Download Client Configuration and put the credentials.json file in the same directory that will hold your future Python script.
Don’t give these credentials away. Do not commit it to a public Git repository. To be safe, never commit it, and add it to your .gitignore file.
If you think some credentials may have been compromised, you can disable it via the Google developers console. You can also see traffic and other information from here.
Armed with your credentials, simply create the script quickstart.py, and copy and paste the quickstart example into it. This example fetches the labels from your Gmail and prints them out.
The first time you run it, your browser will launch and ask you to sign into your Google account, then to approve “Quickstart” to access your account.
The browser should say:
The authentication flow has completed.
Back in your Python project, it should print out the labels from your Gmail account.
That’s it! This and any future progress on this project can be found on Github.