[September 2020 version] Explains the procedure to use Gmail API with Python

This time, I will explain the procedure to use Gmail API in Python. By the time I use the Gmail API, I often enable the API, create credentials, and install libraries, so I wrote it as a memorandum.

Enable Gmail API on Google Cloud Platform

To use the Gmail API, you need to enable the API on Google Cloud Platform (GCP). Please access GCP from here (https://console.cloud.google.com/).

When you access for the first time, the following screen will appear, so agree to the terms of use and click "Agree and execute". Gmail API1 (2).png

From "Select Project" Gmail API2 (2).png

Click New Project. Gmail API3 (2).png

You need to set the project name, so give it a descriptive name. After entering, click "Create". Gmail API4 (3).png

The project is now created.

Next, we will enable the Gmail API. When you type "gmail" in the search window, Gmail API is displayed, so click it. Gmail API5 (2).png

The project selection screen will appear, so click on the project you just created. Gmail API6 (2).png

Click "Enable" in the Gmail API. Gmail API7 (2).png

The Gmail API is now enabled.

Creation of authentication information and OAuth consent screen

Next, set up to get permission to use Gmail from users who use this program. If the program alone can send an email without permission from the user, it may be abused. Therefore, it is necessary to set to get permission from the user when the program is executed.

If the screen below appears, click "Create Credentials". Gmail API8 (2).png

On the "Add Credentials to Project" screen, make the settings as shown below. Since we will be using the API from a Python script this time, we have selected "Other UI (Windows, CLI tools, etc.)" as the location to call the API. Also, I want to get the information of the user's received mail, so I selected "User data" as the type of data I want to access. After completing the settings, click "Required Credentials". Gmail API9 (2).png

A dialog will appear. Click "Set consent screen". Gmail API10 (2).png

"When you select the" OAuth consent screen "tab in the sidebar, the following screen will appear. Select" External "and click" Create ". Gmail API11 (2).png

Set an appropriate name in "Application name" and save. Gmail API12 (3).png

Once this is done, you will be returned to the authentication information creation screen.

Creating an OAuth client ID

Select the Credentials tab and click Create Credentials. Select "OAuth Client ID" from the displayed menu. Gmail API14 (2).png

Select "Desktop App" in "Application Type" and give an appropriate name. Click "Create" when you can. Gmail API15 (2).png

A dialog for completing the creation will be displayed, so click "OK". Gmail API16 (2).png

After transitioning to the screen below, download the JSON file of the authentication information. Gmail16 (2).png

Install Google Client Library

Let's install the library required to use the Gmail API with Python with pip. pip is a Python package management tool that allows you to easily load and use external libraries. Open a command prompt for Windows and a terminal for Mac and execute the following commands.

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

Now you're ready to use the Gmail API.

Summary

I explained the procedure to use Gmail API in Python. If you use the Gmail API, you can automate the sending of emails, so please refer to it if you have work to send emails on a regular basis.

Recommended Posts

[September 2020 version] Explains the procedure to use Gmail API with Python
[Python] Explains how to use the format function with an example
Procedure to use TeamGant's WEB API (using python)
Specify the Python executable to use with virtualenv
The easiest way to use OpenCV with python
[Python] Explains how to use the range function with a concrete example
Use Trello API with python
Use Twitter API with Python
Let's use the Python version of the Confluence API module.
Specify MinGW as the compiler to use with Python
Call the API with python3.
Use subsonic API with python3
To automatically send an email with an attachment using the Gmail API in Python
Easy to use Nifty Cloud API with botocore and python
The first API to make with python Djnago REST framework
[Python] I want to use the -h option with argparse
Python: How to use async with
How to get the Python version
Hit the Etherpad-lite API with Python
How to use OpenPose's Python API
Use the Flickr API from Python
How to use FTP with Python
[Python] How to use Typetalk API
[Python] Create API to send Gmail
Sample to use after OAuth authentication of BOX API with Python
How to send a request to the DMM (FANZA) API with python
[Introduction to Python] Let's use foreach with Python
The road to compiling to Python 3 with Thrift
ImportError when trying to use gcloud package with AWS Lambda Python version
If you want to include awsebcli with CircleCI, specify the python version
Use python on Raspberry Pi 3 to light the LED with switch control!
I tried to get the authentication code of Qiita API with Python.
I tried to get the movie information of TMDb API with Python
How to use the C library in Python
I want to use MATLAB feval with python
Use the MediaWiki API to get Wiki information
The easiest way to synthesize speech with python
Try to solve the man-machine chart with Python
Until you use the Kaggle API with Colab
Use logger with Python for the time being
Say hello to the world with Python with IntelliJ
How to use the Google Cloud Translation API
How to use the NHK program guide API
[Improved version] Script to monitor CPU with Python
I want to use Temporary Directory with Python2
[Algorithm x Python] How to use the list
Use the Python framework "cocotb" to test Verilog.
Introduction to Python with Atom (on the way)
How to use tkinter with python in pyenv
Get Gmail subject and body with Python and Gmail API
[Python] Mention to multiple people with Slack API
How to use Service Account OAuth and API with Google API Client for python
Try to use up the Raspberry Pi 2's 4-core CPU with Parallel Python
[BigQuery] How to use BigQuery API for Python -Table creation-
Try to solve the programming challenge book with python3
[First API] Try to get Qiita articles with Python
[Python] Use JSON with Python
[Introduction to Python] How to iterate with the range function?
Try to solve the internship assignment problem with Python
The first algorithm to learn with Python: FizzBuzz problem
I tried to touch the CSV file with Python