Repository: phidatahq/phidata
File: libs/agno/agno/tools/google_drive.py (Python)

Code before:


6. Set up environment variables:
   Create a .envrc file in your project root with:
   ```
   export GOOGLE_CLIENT_ID=your_client_id_here
   export GOOGLE_CLIENT_SECRET=your_client_secret_here
   export GOOGLE_PROJECT_ID=your_project_id_here
   export GOOGLE_REDIRECT_URI=http://127.0.0.1/  # Default value
   ```

"""

import mimetypes
from functools import wraps
from os import getenv
from pathlib import Path
from typing import Any, List, Optional, Union

from agno.tools import Toolkit

try:
    from google.auth.transport.requests import Request
    from google.oauth2.credentials import Credentials
    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import Resource, build
    from googleapiclient.http import MediaFileUpload, MediaIoBaseDownload
except ImportError:
    raise ImportError(
        "Google client library for Python not found , install it using `pip install google-api-python-client google-auth-httplib2 google-auth-oauthlib`"
    )


def authenticate(func):
    """Decorator to ensure authentication before executing a function."""

    @wraps(func)
    def wrapper(self, *args, **kwargs):
        if not self.creds or not self.creds.valid:
            self._auth()
        if not self.service:
            self.service = build("drive", "v3", credentials=self.creds)
        return func(self, *args, **kwargs)

    return wrapper


class GoogleDriveTools(Toolkit):
    # Default scopes for Google Drive API access
    DEFAULT_SCOPES = ["https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive.readonly"]
    service: Optional[Resource]

Review comment (security):
```suggestion
"""
Google Drive API integration for file management and sharing.


This module provides functions to interact with Google Drive, including listing,
uploading, and downloading files.
It uses the Google Drive API and handles authentication via OAuth2.

Required Environment Variables:
-----------------------------
- GOOGLE_CLIENT_ID: Google OAuth client ID
- GOOGLE_CLIENT_SECRET: Google OAuth client secret
- GOOGLE_PROJECT_ID: Google Cloud project ID
- GOOGLE_REDIRECT_URI: Google OAuth redirect URI (default: http://localhost)


How to Get These Credentials:
---------------------------
1. Go to Google Cloud Console (https://console.cloud.google.com)
2. Create a new project or select an existing one
3. Enable the Google Drive API:
   - Go to "APIs & Services" > "Enable APIs and Services"
   - Search for "Google Drive API"
   - Click "Enable"

4. Create OAuth 2.0 credentials:
   - Go to "APIs & Services" > "Credentials"
   - Click "Create Credentials" > "OAuth client ID"
   - Enable the OAuth Consent Screen if you haven't already
   - After enabling the Consent Screen, click on "Create Credentials" > "OAuth client ID"
   - You'll receive:
     * Client ID (GOOGLE_CLIENT_ID)
     * Client Secret (GOOGLE_CLIENT_SECRET)
   - The Project ID (GOOGLE_PROJECT_ID) is visible in the project dropdown at the top of the page

5. Add auth redirect URI:
   - Go to https://console.cloud.google.com/auth/clients
   - Add `http://localhost:54718/` as a recognized redirect URI


6. Set up environment variables:
   Create a .envrc file in your project root with:
   ``
   export GOOGLE_CLIENT_ID=your_client_id_here
   export GOOGLE_CLIENT_SECRET=your_client_secret_here
   export GOOGLE_PROJECT_ID=your_project_id_here
   export GOOGLE_REDIRECT_URI=http://localhost:54718/  # Default value
   ``
---

Remember to install the dependencies using `pip install google google-auth-oauthlib`
"""
```

Code after:
   - Add `http://localhost:5050` as a recognized redirect URI OR with http://localhost:{PORT_NUMBER}


6. Set up environment variables:
   Create a .envrc file in your project root with:
   ``
   export GOOGLE_CLIENT_ID=your_client_id_here
   export GOOGLE_CLIENT_SECRET=your_client_secret_here
   export GOOGLE_PROJECT_ID=your_project_id_here
   export GOOGLE_REDIRECT_URI=http://localhost/  # Default value
   export GOOGLE_AUTHENTICATION_PORT=5050  # Port for OAuth redirect
   export GOOGLE_CLOUD_QUOTA_PROJECT_ID=your_quota_project_id_here
   ``

---

Remember to install the dependencies using `pip install google google-auth-oauthlib`

Important Points to Note :
1. The first time you run the application, it will open a browser window for OAuth authentication.
2. A token.json file will be created to store the authentication credentials for future use.

You can customize the authentication port by setting the `GOOGLE_AUTHENTICATION_PORT` environment variable.
This will be used in the `run_local_server` method for OAuth authentication.

"""

import mimetypes
from functools import wraps
from os import getenv
from pathlib import Path
from typing import Any, List, Optional, Union

from agno.tools import Toolkit

try:
    from google.auth.transport.requests import Request
    from google.oauth2.credentials import Credentials
    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient.discovery import Resource, build
    from googleapiclient.http import MediaFileUpload, MediaIoBaseDownload
except ImportError:
    raise ImportError(
        "Google client library for Python not found , install it using `pip install google-api-python-client google-auth-httplib2 google-auth-oauthlib`"
    )


def authenticate(func):
    """Decorator to ensure authentication before executing a function."""

    @wraps(func)