Skip to content

Latest commit

 

History

History
69 lines (50 loc) · 2.4 KB

token_generation.md

File metadata and controls

69 lines (50 loc) · 2.4 KB

Token Generation Guide

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.


Prerequisites

Before starting, ensure you have:

  1. Followed the Google Cloud Setup guide.
  2. Placed your token.json in the appropriate directory.

Important

You must run this locally, as a headless Docker container cannot open a browser window for you to log in.


Method A: Using the Project Environment

If you have already set up the full project locally with all dependencies:

  1. Open your terminal in the project root.
  2. Ensure your requirements.txt dependencies are installed:
    pip install -r requirements.txt
  3. Run the whole project from the app directory:
    uvicorn main:app --reload

Method B: Using a Clean Virtual Environment (Recommended)

If you want to generate the token without setting up the entire project:

  1. Create and activate a virtual environment:
    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
  2. Install only the necessary libraries for the token creation:
    pip install google-auth-oauthlib google-api-python-client
  3. Run the script inside the directory app/src/polling:
    python generate_token.py

The Authentication Flow

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.modify scope). 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.