> ## Documentation Index
> Fetch the complete documentation index at: https://phidatainc.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Google Drive

> Search, read, upload, and download Google Drive files with OAuth or service account authentication.

The first agent is read-only. The second enables file uploads and downloads.

## Prerequisites

Enable the Google Drive API in your Google Cloud project, then choose an authentication method:

| Method          | Use                           | Required environment variables                                  |
| --------------- | ----------------------------- | --------------------------------------------------------------- |
| OAuth           | Interactive local development | `GOOGLE_CLIENT_ID`, `GOOGLE_CLIENT_SECRET`, `GOOGLE_PROJECT_ID` |
| Service account | Headless servers              | `GOOGLE_SERVICE_ACCOUNT_FILE`                                   |

Export your OpenAI API key for both agents: `export OPENAI_API_KEY="your_openai_api_key_here"`.

For OAuth, create Desktop app credentials. The first run opens a browser for consent and caches the token in `token.json`.

For a service account, download its JSON key and share the target Drive files or folders with the service-account email. Set `GOOGLE_DELEGATED_USER` only when using domain-wide delegation. `GOOGLE_CLOUD_QUOTA_PROJECT_ID` is optional for both methods.

```python theme={null}
"""
Google Drive Agent that can search, list, read, upload, and download files using Google Drive.
"""

from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.tools.google.drive import GoogleDriveTools

# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------

# Example 1: Read-only Drive agent (default — upload and download disabled)
read_only_agent = Agent(
    name="Drive Reader Agent",
    model=OpenAIChat(id="gpt-4o"),
    tools=[GoogleDriveTools()],
    description="You are a Google Drive specialist that can search and read files.",
    instructions=[
        "You can search, list, and read files from the user's Google Drive.",
        "When listing or searching files, show the file ID, name, type, and last modified date.",
        "When reading files, summarize the content briefly.",
        "Google Docs and Slides are exported as plain text, Sheets as CSV.",
    ],
    markdown=True,
)

# Example 2: Full Drive agent with upload enabled
full_drive_agent = Agent(
    name="Full Drive Agent",
    model=OpenAIChat(id="gpt-4o"),
    tools=[GoogleDriveTools(upload_file=True, download_file=True)],
    description="You are a Google Drive agent with full read and write capabilities.",
    instructions=[
        "You can search, list, read, upload, and download files from Google Drive.",
        "When uploading files, confirm the file path with the user first.",
        "When downloading files, ask for the destination path.",
        "Show file metadata in a structured markdown format.",
    ],
    markdown=True,
)

# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    # Example 1: List recent files
    read_only_agent.print_response(
        "List the 5 most recent files in my Google Drive",
        stream=True,
    )

    # Example 2: Search for specific file types
    read_only_agent.print_response(
        "Search my Google Drive for spreadsheets",
        stream=True,
    )

    # Example 3: Read a Google Doc and summarize
    # read_only_agent.print_response(
    #     "Read the Google Drive file with ID <FILE_ID> and summarize it",
    #     stream=True,
    # )

    # Example 4: Search files in a specific folder
    # read_only_agent.print_response(
    #     "What files are inside the folder called 'Projects'?",
    #     stream=True,
    # )

    # Example 6: Upload a file (requires full_drive_agent)
    # full_drive_agent.print_response(
    #     "Upload the file at /path/to/document.pdf to my Google Drive",
    #     stream=True,
    # )

    # Example 7: Download a file (requires full_drive_agent)
    # full_drive_agent.print_response(
    #     "Download the file 'report.csv' from my Google Drive to /tmp/report.csv",
    #     stream=True,
    # )
```

## Run the Example

```bash theme={null}
git clone https://github.com/agno-agi/agno.git
cd agno
./scripts/demo_setup.sh
source .venvs/demo/bin/activate

uv pip install -U google-api-python-client google-auth-httplib2 google-auth-oauthlib
export OPENAI_API_KEY="your_openai_api_key_here"

python cookbook/91_tools/google_drive.py
```

Full source: [cookbook/91\_tools/google\_drive.py](https://github.com/agno-agi/agno/blob/main/cookbook/91_tools/google_drive.py)
