# Read Google Drive files with a script tool

Give a Risotto script tool read access to a Google Sheet through your Google Workspace tools connection

Use a script tool to read a Google Sheet when an agent handles a request or an automation runs. This guide exports the first tab as CSV through the Drive API. For a specific tab or cell range, use the Sheets API example below.

## Before you start

- You must be an **Org Admin** or **Manager** in Risotto to create and publish the script tool.
- Set up the [Google Workspace tools connection](https://www.tryrisotto.com/docs/integrations/google-workspace-tools). You need a Google Cloud project, a web application OAuth client, and a Workspace admin account with directory access. A delegated admin is enough.
- You need access to enable APIs and configure OAuth in the Google Cloud project that owns that client.
- Choose a Sheet whose contents the tool's users are allowed to read. Have its file ID ready: it is the part between `/d/` and `/edit` in `https://docs.google.com/spreadsheets/d/FILE_ID/edit`.

> **Note:** Use the **Tools app** card under **Settings > Integrations > Google Workspace**.
> The **Admin API** connection and a [Google Drive knowledge
> index](https://www.tryrisotto.com/docs/integrations/knowledge/google-drive) use separate credentials. Connecting
> either does not give this script tool access to Drive.

## Choose what to read

| What you need                                                       | API to enable     | Scope to approve                                        |
| ------------------------------------------------------------------- | ----------------- | ------------------------------------------------------- |
| Export a Sheet's first tab as CSV, or read other Drive file content | Google Drive API  | `https://www.googleapis.com/auth/drive.readonly`        |
| Read a named tab or a bounded cell range as structured values       | Google Sheets API | `https://www.googleapis.com/auth/spreadsheets.readonly` |

Use only the scope your tool needs. The Sheets scope does not authorize Drive API calls. A metadata-only Drive scope cannot read file content.

> **Warning:** These scopes cover files accessible to the account that gives consent, not
> just the Sheet in this guide. Keep the file ID and range fixed in the code
> when the tool should read only one source. The script's `scopes` array does
> not narrow the Google token to that file or to that call.

Google's `drive.file` scope is a different, per-file authorization model. Pasting a file ID into a script does not grant that access. The setup below uses `drive.readonly`; it does not use Google Picker. See [Google's scope guide](https://developers.google.com/workspace/drive/api/guides/api-specific-auth).

## Set up access

1. **Enable the API in Google Cloud**

   In the [Google Cloud console](https://console.cloud.google.com/), select the project that owns the OAuth client used by the **Tools app** connection. Go to **APIs & Services > Library**, find **Google Drive API**, and click **Enable**.

   For the Sheets API example, enable **Google Sheets API** instead. Keep **Admin SDK API** enabled in either case: Risotto uses it to check the Workspace admin connection.

2. **Configure OAuth access**

   In the same project, open **Google Auth Platform** and review **Audience** and **Data Access**. Add the scope from the table above to your app's declared scopes as needed. Keep the existing scopes for the tools connection.

   For an app used only within your Workspace organization, use an organization-owned project with an **Internal** audience. If you use an external app, review Google's verification requirements before using it in production. Google classifies `drive.readonly` as restricted and `spreadsheets.readonly` as sensitive. Restricted scopes can require a security assessment; internal apps can qualify for an exception.

   See Google's [OAuth configuration guide](https://developers.google.com/workspace/guides/configure-oauth-consent) and [verification requirements](https://developers.google.com/identity/protocols/oauth2/production-readiness/restricted-scope-verification).

3. **Share the Sheet with the consenting account**

   On the **Tools app** card in Risotto, check which Workspace account connected the app. Share the Sheet with that account as a **Viewer**, or confirm it already has access through its group or shared drive membership.

   Being a Workspace admin does not grant access to every user's Drive files. For a CSV export, the file must also allow that account to download its contents.

4. **Create and save the script tool**

   In Risotto, open **Tools > New script tool**. Name it for the source it reads, such as **Read software catalog**. Add a description that tells agents when to use it, select the department that will use it, and click **Create and open studio**.

   Click **Code** and replace the starter code with one of the examples below. Replace `YOUR_SPREADSHEET_ID` with the file ID. For the Sheets example, also set the tab and range. Click **Save draft**.

   You can also ask the studio assistant to create the script from the example. Review the code before you publish it.

### Script examples

Both examples read a fixed source and take no parameters. They use the tools connection, so you do not need to copy an access token or add an authorization header.

#### Drive API: export CSV

This example returns the first tab as CSV. Google Sheets are native Workspace documents: use `files.export`, not `files.get?alt=media`. CSV and TSV exports include only the first tab. See Google's [export formats](https://developers.google.com/workspace/drive/api/guides/ref-export-formats).

```typescript
export const input = z.object({});

export const domains = {
  "www.googleapis.com": "read"
};

export default async function run() {
  const fileId = "YOUR_SPREADSHEET_ID";

  // **Read the Sheet as CSV**
  const response = await http.get(
    `https://www.googleapis.com/drive/v3/files/${encodeURIComponent(fileId)}/export`,
    {
      query: { mimeType: "text/csv" },
      auth: {
        type: "google",
        scopes: ["https://www.googleapis.com/auth/drive.readonly"]
      }
    }
  );

  if (response.status_code !== 200) {
    fail(`Google Drive could not export the Sheet (HTTP ${response.status_code}).`);
  }

  return typeof response.body === "string"
    ? response.body
    : JSON.stringify(response.body);
}
```

#### Sheets API: read a range

Use this example to read the first 100 rows of a named tab. Replace `'Software catalog'!A1:D100` with your tab name and range in [A1 notation](https://developers.google.com/workspace/sheets/api/guides/concepts#cell). The tool returns the cell values as a JSON string. Empty trailing rows and columns are omitted.

```typescript
export const input = z.object({});

export const domains = {
  "sheets.googleapis.com": "read"
};

export default async function run() {
  const spreadsheetId = "YOUR_SPREADSHEET_ID";
  const range = "'Software catalog'!A1:D100";

  // **Read the catalog range**
  const response = await http.get(
    `https://sheets.googleapis.com/v4/spreadsheets/${encodeURIComponent(spreadsheetId)}/values/${encodeURIComponent(range)}`,
    {
      auth: {
        type: "google",
        scopes: ["https://www.googleapis.com/auth/spreadsheets.readonly"]
      }
    }
  );

  if (response.status_code !== 200) {
    fail(`Google Sheets could not read the range (HTTP ${response.status_code}).`);
  }

  return JSON.stringify(response.body.values ?? []);
}
```

## Approve the scope and test the tool

1. **Publish the tool while it is disabled**

   Click **Publish** in the studio. Keep the tool disabled until you finish the test.

   A warning that Google does not include the scope is expected at this point. It does not block publication. You must publish first: only published scripts add their scopes to the next consent request. A published tool still contributes its scopes while disabled.

2. **Reconnect the Tools app**

   Go to **Settings > Integrations > Google Workspace**. On the **Tools app** card, click **Connected > Reconnect...**. Review the missing scope, then click **Authorize**.

   Sign in with the same Workspace admin account and approve the requested scopes. When Risotto returns to the integration page, confirm that the missing-scopes warning is gone. You do not need to disconnect the app or create a new OAuth client.

   If you have not connected the **Tools app** yet, use **Set up** or **Connect** and complete the [connection guide](https://www.tryrisotto.com/docs/integrations/google-workspace-tools#set-up-the-connector) after publishing the script.

3. **Run a studio test**

   Return to the tool's studio and click **Test**. These examples have no input parameters. Run the test and check the read step's result: it should contain the expected CSV or cell values from your Sheet.

   A studio test makes a real request to Google and records the result in the run trace. Use sample data for the first test. Access is checked as the connected Workspace account, even when someone else runs the tool.

4. **Enable the tool and use it**

   Enable the tool on its detail page. Add it to a [runbook](https://www.tryrisotto.com/docs/dashboard/runbooks) or [automation](https://www.tryrisotto.com/docs/dashboard/automations), with instructions for when to read the Sheet and how to use the result.

   Reconnecting does not require another publish. Each run reads Google directly; it does not wait for a knowledge index to refresh.

## Troubleshooting

| What you see                                  | What to check                                                                                                                                                                                                                                        |
| --------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| The scope is still missing                    | Publish the saved draft, then reconnect the **Tools app** and approve the scope. A draft alone does not change the consent request.                                                                                                                  |
| Google says the API is disabled               | Enable Drive API or Sheets API in the project that owns the tools OAuth client. Admin SDK must stay enabled too.                                                                                                                                     |
| Google returns HTTP 403                       | Check the approved scope, the connected account's file access, download restrictions, and your Workspace app access policy.                                                                                                                          |
| Google returns HTTP 404                       | Check the file ID and confirm the connected Workspace account can open the Sheet. Google can return 404 when that account has no access.                                                                                                             |
| The Sheets API returns HTTP 400               | Check the tab name and A1 range. Keep single quotes around a tab name that contains spaces.                                                                                                                                                          |
| CSV contains the wrong tab                    | Drive exports only the first tab as CSV. Use the Sheets API example to select a tab.                                                                                                                                                                 |
| The response is too large                     | Script HTTP responses are limited to 8 MiB. Read a smaller range through the Sheets API. Drive exports also have Google's [10 MB export limit](https://developers.google.com/workspace/drive/api/reference/rest/v3/files/export).                    |
| The connection stops working after seven days | If the OAuth app is External and in Testing, Google limits its refresh token lifetime. Configure the app for production use under Google's [OAuth requirements](https://developers.google.com/identity/protocols/oauth2#expiration), then reconnect. |

If the connected account changes or loses file access, check sharing again before you rerun the tool.

Source: https://www.tryrisotto.com/docs/guides/read-google-drive-files
