> For the complete documentation index, see [llms.txt](https://docs.stackai.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.stackai.com/integrations/catalog/onedrive/connect-onedrive.md).

# Connect OneDrive

StackAI integrates with Microsoft OneDrive. Two authentication methods are supported:

* **OAuth2 (delegated)** for user-account access
* **App API / Client Credentials (app-only)** for background services and automation

***

### Setup an OAuth2 Connection

This method authenticates as a specific user. The connection acts on behalf of the user who completes the OAuth flow, and only has access to files that user can see.

You can set up a connection at Connections > OneDrive > OAuth Connection

![Connect OneDrive setup](https://1957915746-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSQ1Sgy933krTyK0S4JpP%2Fuploads%2FbkIQs6OgKl6TmLDxDTN7%2Fimage.png?alt=media)

***

### Set up an App API Connection

This method authenticates as the application itself, not as any user. It is used to connect to services accounts or perform background automation where no interactive login is possible. A **User Principal Name (UPN)** must be supplied to specify which user's OneDrive to access.

#### Step 1: Create an Azure App Registration

1. Go to [Azure Portal > App registrations](https://portal.azure.com/#view/Microsoft_AAD_RegisteredApps/ApplicationsListBlade) and click **New registration**.
2. Set a name (e.g., `StackAI OneDrive`).
3. Under **Supported account types**, choose the appropriate option:
   * **Single tenant** — only users in your Azure AD directory.
   * **Multitenant** — users in any Azure AD directory. Use `common` as the Tenant ID in StackAI.
4. **Redirect URI** is not required.
5. Click **Register**.

#### Step 2: Create Client Credentials

1. In your app registration, go to **Certificates & secrets > Client secrets**.
2. Click **New client secret**, set a description and expiry, and click **Add**.
3. Copy the **Value** immediately. It is only shown once.

#### Step 3: Configure API Permissions

Go to **API permissions > Add a permission > Microsoft Graph > Application permissions** and add the following:

<table><thead><tr><th width="197.48046875">Permission</th><th width="124.9375">Type</th><th>Purpose</th></tr></thead><tbody><tr><td><code>Files.Read.All</code></td><td>Application</td><td>Read all files in OneDrive across users</td></tr><tr><td><code>Files.ReadWrite.All</code></td><td>Application</td><td>Read and write all files across users (only needed if your workflow modifies files)</td></tr><tr><td><code>User.Read.All</code></td><td>Application</td><td>Read all users' full profiles (required to resolve the User Principal Name)</td></tr></tbody></table>

{% hint style="info" %}
Application permissions are more powerful than delegated permissions because they are not scoped to a single user. Always apply the [principle of least privilege](https://learn.microsoft.com/en-us/entra/identity-platform/secure-least-privileged-access) — only grant `ReadWrite` if your workflow needs to modify files.
{% endhint %}

After adding permissions, click **Grant admin consent**. Admin consent is **required** for all application permissions.

#### Step 4: Connect in StackAI

In the StackAI connector setup, provide:

<table><thead><tr><th width="210.328125">Field</th><th>Value</th></tr></thead><tbody><tr><td><strong>Client ID</strong></td><td>Application (client) ID from your app registration's <strong>Overview</strong> page</td></tr><tr><td><strong>Client Secret</strong></td><td>The client secret value</td></tr><tr><td><strong>Tenant ID</strong></td><td>Your Azure AD tenant ID</td></tr><tr><td><strong>User Principal Name</strong></td><td>The email/UPN of the OneDrive user to access (e.g., <code>user@yourdomain.com</code>)</td></tr></tbody></table>

***

### Troubleshooting

#### Admin consent not granted

Application permissions require admin consent before they work. If you see `403 Forbidden` or `insufficient_scope` errors, go to **API permissions** and click **Grant admin consent for \[your tenant]**.

#### Invalid Tenant ID for client credentials

The `common` Tenant ID is only valid for OAuth2 (delegated) flows. For client credentials (App API), you must provide your specific Azure AD Tenant ID (a UUID).

#### Token expired

OAuth2 tokens expire after \~1 hour. StackAI automatically uses the refresh token to obtain a new access token. Ensure `offline_access` is included in your OAuth2 scopes so refresh tokens are issued.

#### Accessing files across users (App-Only)

With client credentials, you must specify a **User Principal Name** to scope access to a particular user's OneDrive. The app identity itself does not have a personal drive.

## Connection fields and action reference

See [OneDrive](https://docs.stackai.com/integrations/catalog/onedrive.md) for the current connector fields and supported actions.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.stackai.com/integrations/catalog/onedrive/connect-onedrive.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
