Code based on own experience and some codes I couldn't find the sources, sorry. Python Gmail IMAP was written to help community with code that isn't clearly documented. You can find a step by step on how to get the credentials you'll need to run the code.
It's not meant to be an IMAP client, instead, just a piece of code to built in your projects.
Firstly, clone the repository and access it's directory:
git clone https://github.com/rcbonz/Python-Gmail-IMAP.git
cd Python-Gmail-IMAP
python3 pythonGmailImap.py
Follow the steps to get the required credentials on Google Console here and with your Client ID and Client Secret, run the following command on terminal:
python2 oauth2.py --generate_oauth2_token --client_id=<your client id> --client_secret=<your client secret>
The original oauth2.py file can be found here.
This command will generate a link to allow the IMAP to be used on you e-mail account, as follows:
Access the link in a browser and follow the steps till you get the Authorization Code and copy it. Go back to the terminal and paste it:
Now you have the refresh token that you'll need to run the IMAP client.
Paste all credentials in the pythonGmailImap.py
and run it:
python3 pythonGmailImap.py
Access the Google Console page and follow the steps:
- Create a new project and search for the Gmail API on the API Library tab, then open it and Enable it.
- In the Gmail API details tab, click on Credentials and than in Create Credentials. Choose the OAuth client ID
- Select Desktop app as the application type and finish creating it.
- There are your Client ID and Client Secret.