> For the complete documentation index, see [llms.txt](https://docs.appfarm.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.appfarm.io/how-to/integrations/retrieve-access-token-from-microsoft-entra-id.md).

# Retrieve access token from Microsoft Entra ID

This guide describes how to retrieve and store an application access token with a shared secret from Microsoft Entra ID. Access tokens are required to build integrations with Microsoft APIs like Microsoft Graph.

For more information about the configuration required on the Microsoft side, read about the [client credentials flow](https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-client-creds-grant-flow) in the Microsoft Documentation.

In Appfarm, this process requires setting up a [Flow](https://docs.appfarm.io/reference/flows.md) action that updates a [secret](https://docs.appfarm.io/reference/security/secrets.md). The action can update the secret at regular intervals using a [schedule](https://docs.appfarm.io/reference/flows/schedules.md).

{% hint style="info" %}
**Example**

An [example service](https://create.appfarm.io/showroom/services/CJKUWW/endpoints/j6sBbr/) based on Microsoft Sharepoint integration is available in our [Showroom](https://docs.appfarm.io/getting-started/appfarm-showroom).
{% endhint %}

### Step 1

Sign in to the Microsoft Azure [portal](https://portal.azure.com/) using an account with administrator permission.

### Step 2

Register an application.

* Go to **Microsoft Entra ID**.
* Click **Add** -> **App registration**.
* In the **Name** field, enter a display name, for example `appfarm`.
* Click **Register**.

### Step 3

Generate and record credentials.

* Go to **Certificates & secrets**.
* Click **New client secret**.
* Enter the required details and click **Add**.
* Note down the generated client secret **Value**. This value is required later in this process.
* Go to **Overview**.
* Note down the **Application (client) ID** and **Directory (tenant) ID** values.

### Step 4

You can now start configuring the integration in Appfarm Create. First, add the credentials gathered in step 3 as [secrets](https://docs.appfarm.io/reference/security/secrets.md).

* In [Appfarm Create](https://create.appfarm.io), go to **Security** > **Secrets**.
* Click **Create secret**.
* In the **Name** field, enter a name for the client secret credential, for example `Microsoft Client Secret`.
* In the **Value** field, enter the client secret you noted down in Step 3.
* Select **Environment Specific**, so each environment has its own value. Use a separate Entra app registration and client secret for Production.
* Click **Create**.
* Repeat this process, adding the application (client) ID and directory (tenant) ID values as secrets.

### Step 5

Create another secret to store the access token that will be used to authenticate individual requests to the Microsoft API.

* Click **Create secret**.
* In the **Name** field, enter a name for the access token, for example `Microsoft Access Token`.
* Select **Environment Specific**. Update secret can only update environment-specific secrets.
* Click **Create**.

The value of this access token secret will be updated by the Flow action. The service account that runs the schedule needs permission to update it:

* Go to **Security** > **Permissions** > **Advanced**.
* Under **Update Secret from Service**, select the access token secret for the role of the service account that will run the schedule.

### Step 6

Create a [Flow](https://docs.appfarm.io/reference/flows.md). You can also choose to use an existing Flow.

* Go to **Flows**.
* Create a new Flow and give it a name, for example `Microsoft Entra ID`.
* Add an action to the Flow, and give it a name, for example `Get access token`.

### Step 7

Create an action variable. The action variable will be used to temporarily store a new access token before it is updated in the secret.

* In the action, go to **Action data**.
* Under **Action Variables**, add a variable.
* In the **Name** field, enter a name for the action variable, for example `Access Token`, and keep **Data Type** `String`.

### Step 8

Add the logic that retrieves an access token and updates the secret.

* Go to **Logic** in the action.
* Add a [Web request action node](https://docs.appfarm.io/library/action-nodes/web-request.md). The web request will request a new access token and store it in the action variable.
  * Configure the web request with the following properties:

<table><thead><tr><th width="131">Property</th><th width="659">Value</th></tr></thead><tbody><tr><td><strong>Base URL</strong></td><td><code>https://login.microsoftonline.com</code></td></tr><tr><td><strong>Path</strong></td><td><code>{{tenantId}}/oauth2/v2.0/token</code></td></tr><tr><td><strong>Path parameters</strong></td><td><ul><li>Add a path parameter with <strong>Key</strong> <code>tenantId</code>.</li><li>For <strong>Value</strong>, select the directory (tenant) ID secret created in step 4.</li></ul></td></tr><tr><td><strong>Method</strong></td><td><code>POST</code></td></tr><tr><td><strong>Body Type</strong></td><td><code>URL-encoded</code></td></tr><tr><td><strong>Body Content</strong></td><td><ul><li><p>Add form data entries with the following key-value pairs.</p><ul><li><strong>Key</strong>: <code>grant_type</code>, <strong>Value</strong>: <code>client_credentials</code></li><li><strong>Key</strong>: <code>client_id</code>, <strong>Value</strong>: Use the function editor to return the secret storing the application (client) ID created in step 4.</li><li><strong>Key</strong>: <code>client_secret</code>, <strong>Value</strong>: Use the function editor to return the secret storing the client secret created in step 4.</li><li><strong>Key</strong>: <code>scope</code>, <strong>Value</strong>: See the <a href="https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-client-creds-grant-flow#first-case-access-token-request-with-a-shared-secret">Microsoft documentation</a>. For Microsoft Graph integration, you might set <code>https://graph.microsoft.com/.default</code>.</li></ul></li></ul></td></tr><tr><td><strong>Response Type</strong></td><td><code>JSON (default)</code></td></tr><tr><td><strong>Result Mapping</strong></td><td><ul><li>Click <strong>Add Result Mapping</strong>.</li><li>For the <strong>Data Source</strong> property, select <code>Action Variables</code>.</li><li>Under <strong>Property Mapping</strong>, for the action variable created in step 7, enter <code>access_token</code>.</li></ul></td></tr></tbody></table>

* Add an [If action node](https://docs.appfarm.io/library/action-nodes/if.md).
  * Configure the condition to check if the action variable created in [step 7](#step-7) has a value.

<figure><img src="https://29237295-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MiLU-xcHu0eLZiTxcmZ%2Fuploads%2FvobXeJDSOqJDde3SY7HM%2Fmicrosoft_retrieve_access_token_action_if.png?alt=media&amp;token=b1fd4c12-dc1a-4cc0-a604-64c8fa6abe4f" alt="" width="563"><figcaption><p>Example condition configuration</p></figcaption></figure>

* Add an [Update secret action node](https://docs.appfarm.io/library/action-nodes/update-secret.md) inside the If action node you just added.
  * For the **Secret** property, select the access token secret created in [step 5](#step-5).
  * For the **Value** property, select the action variable created in [step 7](#step-7).

<figure><img src="https://29237295-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MiLU-xcHu0eLZiTxcmZ%2Fuploads%2FOCSt8cs5tAF3KYWtJN5s%2Fmicrosoft_retrieve_access_token_action_update_secret.png?alt=media&amp;token=764e40f9-3a4c-451f-bc62-cecdf5a2062b" alt="" width="346"><figcaption><p>Example update secret configuration</p></figcaption></figure>

### Step 9

Schedule the action to refresh the access token automatically.

* Under **Triggers** in the action, click **set up a schedule**.
* In **Schedule**, enter a cron expression. Microsoft access tokens usually expire after one hour, so run the action more often than that, for example `*/30 * * * *`.
* For **Service Account**, select the service account whose role you gave permission in step 5.
* Select **Enabled**, and click **Create**.

See [Schedules](https://docs.appfarm.io/reference/flows/schedules.md) for details.

The secret can then be used as the bearer token value for authentication when you make API calls using the [web request action node](https://docs.appfarm.io/library/action-nodes/web-request.md).


---

# 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.appfarm.io/how-to/integrations/retrieve-access-token-from-microsoft-entra-id.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.
