To authenticate the system with the Gmail API, you must generate a token.pickle file. This process must be performed on your local machine (not via Docker) because the script needs to open a physical web browser window to complete the Google OAuth2 flow.
Before starting, ensure you have:
- Followed the Google Cloud Setup guide.
- Placed your
token.jsonin the appropriate directory.
Important
You must run this locally, as a headless Docker container cannot open a browser window for you to log in.
If you have already set up the full project locally with all dependencies:
- Open your terminal in the project root.
- Ensure your
requirements.txtdependencies are installed:pip install -r requirements.txt
- Run the whole project from the app directory:
uvicorn main:app --reload
If you want to generate the token without setting up the entire project:
- Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
- Install only the necessary libraries for the token creation:
pip install google-auth-oauthlib google-api-python-client
- Run the script inside the directory app/src/polling:
python generate_token.py
Regardless of the method chosen, the following will happen during the process:
- Browser Redirection: A tab will automatically open in your default web browser.
- Google Login: You must log in using the exact same Gmail account that you added as a "Test User" in the Google Cloud Console.
- Permissions: You will see a prompt asking for permission to access your Gmail data (specifically the
gmail.modifyscope). Click Allow. - Completion: Once the browser displays the message "The authentication flow has completed," you can safely close the tab.
The script will then generate the token.pickle file in your local directory, you will need to move it into the app directory:
mv token.pickle ../../Caution
If you ever change the API Scopes in your code, you must delete the existing token.pickle and repeat this local process to grant the new permissions.