# OneDrive

> Source: https://truto.one/docs/integration-guides/onedrive/

OneDrive connects through Microsoft Graph when a user signs in with their Microsoft work or school account (OAuth 2.0), through Truto's Microsoft app or your own. Truto can also tell you when anything in a connected drive changes.

## Install the OneDrive integration

Install OneDrive from [Available integrations](https://app.truto.one/integrations/available/ccece299-4808-4117-9f8d-cb1370d077ba) in each environment you connect accounts from.

## Use your own OneDrive OAuth app (optional)

Truto ships a OneDrive OAuth app, so OAuth 2.0 works without any setup. To show your own app on the consent screen instead, register an app in Microsoft Entra ID that allows accounts in any organizational directory, register the redirect URL below, and set the app's Client ID and Client Secret on the installed integration. See [Overriding OAuth credentials](/docs/guides/integrations/overriding-oauth-credentials).

```text
https://api.truto.one/connect/onedrive/callback
```

If you [use a redirect URL on your own domain](/docs/guides/integrations/overriding-oauth-credentials#use-a-redirect-url-on-your-own-domain), register that URL instead.

By default, Truto asks OneDrive for these scopes. The Connect UI lists them under **Permissions required**, and you can change them when you override the OAuth credentials.

- `email`
- `offline_access`
- `openid`
- `profile`
- `https://graph.microsoft.com/Files.Read.All`
- `https://graph.microsoft.com/Files.ReadWrite.All`
- `https://graph.microsoft.com/User.Read`
- `https://graph.microsoft.com/User.Read.All`
- `https://graph.microsoft.com/Directory.Read.All`

Enable the same scopes on your app. If your app allows fewer, change the list under **Use your own OAuth settings** → **Scopes** on the installed integration's **Configuration** tab to match.

## Connect a OneDrive account

1. In [Integrated Accounts](https://app.truto.one/integrated-accounts), click **Connect Account**.
   ![Click Connect Account](https://docs-assets.truto.one/truto-connect-account.png)
2. Enter a **Tenant ID** (your customer's ID; Truto creates the tenant if it is new), or pick one under **Existing tenant (optional)**. Check the **Environment** and the **Region** (where Truto places the account; Western North America by default). To open the link straight on OneDrive, pick it under **Advanced settings** → **Integration (optional)**. Click **Get connection link**.
   ![Get connection link](https://docs-assets.truto.one/truto-connect-account-modal.png)
3. Click the open-in-new-tab button, or click **Copy** and send the link to your customer so they connect their own OneDrive account. The link works for 7 days, until an account is connected with it.
   ![Share connection link](https://docs-assets.truto.one/truto-connect-account-link.png)
4. Click **Get Started** if it shows, then select **OneDrive**.
5. Sign in with a Microsoft account and allow access. Only work or school accounts can sign in. `User.Read.All` and `Directory.Read.All` need an administrator's consent, so an admin may have to approve the app first.
6. Close the tab when you see **Connection successful**. The new account appears under **Integrated Accounts**. Click its Account ID to see its details and make API calls.

## Get notified when a drive changes

Microsoft Graph tells Truto when anything in a watched drive changes, and Truto sends a `record:updated` event to your webhooks. The event says which drive changed, not what changed; call `track` to fetch the changes.

1. Install the **File Storage** Unified API in the same environment. Truto turns OneDrive notifications into `record:*` events only through it.
2. [Create a Truto webhook](/docs/guides/webhooks/creating-a-webhook-through-ui) subscribed to `record:updated` and `record:meta` (or `all`).
3. Store a webhook secret on the integrated account, a random string of up to 128 characters. Truto sends it to Microsoft with the subscription and rejects every notification that doesn't carry it, so without a secret no events arrive.

   ```bash
   curl -X PATCH https://api.truto.one/integrated-account/<integrated_account_id>/credentials \
     -H "Authorization: Bearer $TRUTO_API_TOKEN" \
     -H "Content-Type: application/json" \
     -d '{"context": {"webhook_secret": "<random string>"}}'
   ```

4. Find the drive's ID with `GET /unified/file-storage/drives?integrated_account_id=<integrated_account_id>`, then create the subscription:

   ```bash
   curl -X POST "https://api.truto.one/unified/file-storage/webhook-subscriptions?integrated_account_id=<integrated_account_id>" \
     -H "Authorization: Bearer $TRUTO_API_TOKEN" \
     -H "Content-Type: application/json" \
     -d '{
       "notification_url": "https://api.truto.one/integrated-account-webhook/<integrated_account_id>",
       "drive": { "id": "<drive_id>" }
     }'
   ```

   The subscription watches the whole drive. Microsoft lets it live at most 42,300 minutes (about 29 days); read `expiration` from the response. Because `notification_url` points at Truto, Truto also receives Microsoft's renewal notices, renews the subscription for you and sends a `record:meta` event.
5. Each `record:updated` event has `resource` `file-storage/drive-items` and a record whose `drive.id` names the drive that changed. Call `track` for that drive with the last `sync_token` you stored:

   ```bash
   curl -g -X POST "https://api.truto.one/unified/file-storage/drive-items/track?integrated_account_id=<integrated_account_id>&drive[id]=<drive_id>&sync_token=<sync_token>" \
     -H "Authorization: Bearer $TRUTO_API_TOKEN"
   ```

   Every record carries the `sync_token` to send next and `has_more`. Keep calling while `has_more` is `true`, and store the newest `sync_token`. Deleted items come back with `deleted: true`. Leave out `sync_token` to list every item in the drive, or pass `latest` to start from now. Microsoft may send more than one notification for one change, so treat each event as "call `track`".

To stop, call `DELETE /unified/file-storage/webhook-subscriptions/<subscription_id>?integrated_account_id=<integrated_account_id>`. See [Receiving vendor webhooks](/docs/guides/webhooks/inbound-vendor-webhooks) for the event format.
