# SharePoint: an app with a client secret

With **Application Permission**, a [SharePoint](VFS-SharePoint.md) item acts as the app itself. Nobody signs in: the server shows Microsoft the app's ID and a secret, and is let in with the rights the app was granted. This is the usual way for a server that runs unattended.

## At Microsoft

1. Register an app: see [The app registration](VFS-SharePoint.md#the-app-registration-for-all-three). No redirect URI is needed for this way.
2. Under **API permissions**, **Add a permission**, **Microsoft Graph**, **Application permissions**, and choose one:

   | Permission | Gives the app |
   |---|---|
   | `Sites.Selected` | nothing by itself: only the sites you then grant it, see below. The one to prefer. |
   | `Files.ReadWrite.All` | reading and writing of all files in all sites and all OneDrives of the tenant |
   | `Sites.FullControl.All` | everything in all sites, including their settings and permissions |

3. Press **Grant admin consent**. Application permissions always need it.
4. Under **Certificates & secrets**, make a **New client secret**. Copy its **value** at once; it is shown only then. Note when it expires.

## In the item

1. Choose the item type **SharePoint** and the choice **Application Permission**.
2. A window, "Please enter Sharepoint Oauth", asks for **Client ID**, **Client Secret** and **Tenant**. Enter the application ID, the secret's value and the tenant ID.
3. The item is filled in: the **User name** is `app_permission~` followed by the client ID, the **Password** is the secret.
4. Say where: paste a **Shared Link**, or fill in the site and library. See [Where in SharePoint](VFS-SharePoint.md#where-in-sharepoint).
5. Press **Test**.

## Only some sites

`Files.ReadWrite.All` opens the whole tenant to whoever holds the secret. With `Sites.Selected`, the app can reach only the sites an administrator names:

1. Give the app the application permission `Sites.Selected`, with admin consent.
2. For each site, an administrator of SharePoint grants the app a role on it, **read**, **write** or more. This is done with Microsoft Graph, by a request to the site's `permissions`, or with the PowerShell tools for SharePoint; Microsoft describes both under "Sites.Selected".
3. In the item, name the site exactly: the app cannot search for sites, so use **Shared Link** or the site's path.

A stolen secret then opens two sites, not two thousand.

## The secret expires

A client secret has an end date, at most two years away. On that day the item stops working, for all users at once.

- Write the date down where the team sees it.
- Make the new secret some weeks before, open the item, choose **Application Permission** again and enter it.
- If the item uses a [connection profile](Connection-Profiles.md), you change it in one place.

A certificate is longer-lived and never travels: see [An app with a certificate](VFS-SharePoint-App-Certificate.md).

## When it does not work

| What you see | Usually |
|---|---|
| "AADSTS7000215: invalid client secret" | the secret's ID was entered instead of its value; or it expired |
| "AADSTS700016: application not found" | the client ID, or the tenant |
| "access denied" | no admin consent; a delegated permission instead of an application permission; with `Sites.Selected`, no grant on this site |
| it lists the site and cannot write | the app has only the role **read** on the site |
