|
| 1 | +--- |
| 2 | +title: Create authorization with Microsoft Graph API - Azure API Management | Microsoft Docs |
| 3 | +description: Learn how to create and use an authorization to the Microsoft Graph API in Azure API Management. An authorization manages authorization tokens to an OAuth 2.0 backend service. |
| 4 | +services: api-management |
| 5 | +author: dlepow |
| 6 | +ms.service: api-management |
| 7 | +ms.topic: how-to |
| 8 | +ms.date: 04/10/2023 |
| 9 | +ms.author: danlep |
| 10 | +--- |
| 11 | + |
| 12 | +# Create an authorization with the Microsoft Graph API |
| 13 | + |
| 14 | +This article guides you through the steps required to create an [authorization](authorizations-overview.md) with the Microsoft Graph API within Azure API Management. The authorization code grant type is used in this example. |
| 15 | + |
| 16 | +You learn how to: |
| 17 | + |
| 18 | +> [!div class="checklist"] |
| 19 | +> * Create an Azure AD application |
| 20 | +> * Create and configure an authorization in API Management |
| 21 | +> * Configure an access policy |
| 22 | +> * Create a Microsoft Graph API in API Management and configure a policy |
| 23 | +> * Test your Microsoft Graph API in API Management |
| 24 | +
|
| 25 | +## Prerequisites |
| 26 | + |
| 27 | +- Access to an Azure Active Directory (Azure AD) tenant where you have permissions to create an app registration and to grant admin consent for the app's permissions. [Learn more](../active-directory/roles/delegate-app-roles.md#restrict-who-can-create-applications) |
| 28 | + |
| 29 | + If you want to create your own developer tenant, you can sign up for the [Microsoft 365 Developer Program](https://developer.microsoft.com/microsoft-365/dev-program). |
| 30 | +- A running API Management instance. If you need to, [create an Azure API Management instance](get-started-create-service-instance.md). |
| 31 | +- Enable a [system-assigned managed identity](api-management-howto-use-managed-service-identity.md) for API Management in the API Management instance. |
| 32 | + |
| 33 | +## Step 1: Create an Azure AD application |
| 34 | + |
| 35 | +Create an Azure AD application for the API and give it the appropriate permissions for the requests that you want to call. |
| 36 | + |
| 37 | +1. Sign into the [Azure portal](https://portal.azure.com/) with an account with sufficient permissions in the tenant. |
| 38 | +1. Under **Azure Services**, search for **Azure Active Directory**. |
| 39 | +1. On the left menu, select **App registrations**, and then select **+ New registration**. |
| 40 | + :::image type="content" source="media/authorizations-how-to-azure-ad/create-registration.png" alt-text="Screenshot of creating an Azure AD app registration in the portal."::: |
| 41 | + |
| 42 | +1. On the **Register an application** page, enter your application registration settings: |
| 43 | + 1. In **Name**, enter a meaningful name that will be displayed to users of the app, such as *MicrosoftGraphAuth*. |
| 44 | + 1. In **Supported account types**, select an option that suits your scenario, for example, **Accounts in this organizational directory only (Single tenant)**. |
| 45 | + 1. Set the **Redirect URI** to **Web**, and enter `https://authorization-manager.consent.azure-apim.net/redirect/apim/<YOUR-APIM-SERVICENAME>`, substituting the name of the API Management service where you will configure the authorization provider. |
| 46 | + 1. Select **Register**. |
| 47 | +1. On the left menu, select **API permissions**, and then select **+ Add a permission**. |
| 48 | + :::image type="content" source="./media/authorizations-how-to-azure-ad/add-permission.png" alt-text="Screenshot of adding an API permission in the portal."::: |
| 49 | + |
| 50 | + 1. Select **Microsoft Graph**, and then select **Delegated permissions**. |
| 51 | + > [!NOTE] |
| 52 | + > Make sure the permission **User.Read** with the type **Delegated** has already been added. |
| 53 | + 1. Type **Team**, expand the **Team** options, and then select **Team.ReadBasic.All**. Select **Add permissions**. |
| 54 | + 1. Next, select **Grant admin consent for Default Directory**. The status of the permissions will change to **Granted for Default Directory**. |
| 55 | +1. On the left menu, select **Overview**. On the **Overview** page, find the **Application (client) ID** value and record it for use in Step 2. |
| 56 | +1. On the left menu, select **Certificates & secrets**, and then select **+ New client secret**. |
| 57 | + :::image type="content" source="media/authorizations-how-to-azure-ad/create-secret.png" alt-text="Screenshot of creating an app secret in the portal."::: |
| 58 | + |
| 59 | + 1. Enter a **Description**. |
| 60 | + 1. Select any option for **Expires**. |
| 61 | + 1. Select **Add**. |
| 62 | + 1. Copy the client secret's **Value** before leaving the page. You will need it in Step 2. |
| 63 | + |
| 64 | +## Step 2: Configure an authorization in API Management |
| 65 | + |
| 66 | +1. Sign into the [portal](https://portal.azure.com) and go to your API Management instance. |
| 67 | +1. On the left menu, select **Authorizations**, and then select **+ Create**. |
| 68 | + :::image type="content" source="media/authorizations-how-to-azure-ad/create-authorization.png" alt-text="Screenshot of creating an API authorization in the portal."::: |
| 69 | +1. On the **Create authorization** page, enter the following settings, and select **Create**: |
| 70 | + |
| 71 | + |Settings |Value | |
| 72 | + |---------|---------| |
| 73 | + |**Provider name** | A name of your choice, such as *aad-01* | |
| 74 | + |**Identity provider** | Select **Azure Active Directory v1** | |
| 75 | + |**Grant type** | Select **Authorization code** | |
| 76 | + |**Client id** | Paste the value you copied earlier from the app registration | |
| 77 | + |**Client secret** | Paste the value you copied earlier from the app registration | |
| 78 | + |**Resource URL** | `https://graph.microsoft.com` | |
| 79 | + |**Tenant ID** | Optional for Azure AD identity provider. Default is *Common* | |
| 80 | + |**Scopes** | Optional for Azure AD identity provider. Automatically configured from AD app's API permissions. | |
| 81 | + |**Authorization name** | A name of your choice, such as *aad-auth-01* | |
| 82 | + |
| 83 | +1. After the authorization provider and authorization are created, select **Next**. |
| 84 | + |
| 85 | +## Step 3: Authorize with Azure AD and configure an access policy |
| 86 | + |
| 87 | +1. On the **Login** tab, select **Login with Azure Active Directory**. Before the authorization will work, it needs to be authorized. |
| 88 | + :::image type="content" source="media/authorizations-how-to-azure-ad/login-azure-ad.png" alt-text="Screenshot of login with Azure AD in the portal."::: |
| 89 | + |
| 90 | +1. When prompted, sign in to your organizational account. |
| 91 | +1. On the confirmation page, select **Allow access**. |
| 92 | +1. After successful authorization, the browser is redirected to API Management and the window is closed. In API Management, select **Next**. |
| 93 | +1. On the **Access policy** page, create an access policy so that API Management has access to use the authorization. Ensure that a managed identity is configured for API Management. [Learn more about managed identities in API Management](api-management-howto-use-managed-service-identity.md#create-a-system-assigned-managed-identity). |
| 94 | +1. For this example, select **API Management service `<service name>`**. |
| 95 | + |
| 96 | + :::image type="content" source="media/authorizations-how-to-azure-ad/create-access-policy.png" alt-text="Screenshot of selecting a managed identity to use the authorization."::: |
| 97 | + |
| 98 | +1. Select **Complete**. |
| 99 | + |
| 100 | +> [!NOTE] |
| 101 | +> If you update your Microsoft Graph permissions after this step, you will have to repeat Steps 2 and 3. |
| 102 | +
|
| 103 | +## Step 4: Create a Microsoft Graph API in API Management and configure a policy |
| 104 | + |
| 105 | +1. Sign into the [portal](https://portal.azure.com) and go to your API Management instance. |
| 106 | +1. On the left menu, select **APIs > + Add API**. |
| 107 | +1. Select **HTTP** and enter the following settings. Then select **Create**. |
| 108 | + |
| 109 | + |Setting |Value | |
| 110 | + |---------|---------| |
| 111 | + |**Display name** | *msgraph* | |
| 112 | + |**Web service URL** | `https://graph.microsoft.com/v1.0` | |
| 113 | + |**API URL suffix** | *msgraph* | |
| 114 | + |
| 115 | +1. Navigate to the newly created API and select **Add Operation**. Enter the following settings and select **Save**. |
| 116 | + |
| 117 | + |Setting |Value | |
| 118 | + |---------|---------| |
| 119 | + |**Display name** | *getprofile* | |
| 120 | + |**URL** for GET | /me | |
| 121 | + |
| 122 | +1. Follow the preceding steps to add another operation with the following settings. |
| 123 | + |
| 124 | + |Setting |Value | |
| 125 | + |---------|---------| |
| 126 | + |**Display name** | *getJoinedTeams* | |
| 127 | + |**URL** for GET | /me/joinedTeams | |
| 128 | + |
| 129 | +1. Select **All operations**. In the **Inbound processing** section, select the (**</>**) (code editor) icon. |
| 130 | +1. Copy the following, and paste in the policy editor. Make sure the `provider-id` and `authorization-id` correspond to the values you configured in Step 2. Select **Save**. |
| 131 | + |
| 132 | + ```xml |
| 133 | + <policies> |
| 134 | + <inbound> |
| 135 | + <base /> |
| 136 | + <get-authorization-context provider-id="aad-01" authorization-id="aad-auth-01" context-variable-name="auth-context" identity-type="managed" ignore-error="false" /> |
| 137 | + <set-header name="authorization" exists-action="override"> |
| 138 | + <value>@("Bearer " + ((Authorization)context.Variables.GetValueOrDefault("auth-context"))?.AccessToken)</value> |
| 139 | + </set-header> |
| 140 | + </inbound> |
| 141 | + <backend> |
| 142 | + <base /> |
| 143 | + </backend> |
| 144 | + <outbound> |
| 145 | + <base /> |
| 146 | + </outbound> |
| 147 | + <on-error> |
| 148 | + <base /> |
| 149 | + </on-error> |
| 150 | + </policies> |
| 151 | + ``` |
| 152 | +The preceding policy definition consists of two parts: |
| 153 | + |
| 154 | +* The [get-authorization-context](get-authorization-context-policy.md) policy fetches an authorization token by referencing the authorization provider and authorization that were created earlier. |
| 155 | +* The [set-header](set-header-policy.md) policy creates an HTTP header with the fetched authorization token. |
| 156 | + |
| 157 | +## Step 5: Test the API |
| 158 | +1. On the **Test** tab, select one operation that you configured. |
| 159 | +1. Select **Send**. |
| 160 | + |
| 161 | + :::image type="content" source="media/authorizations-how-to-azure-ad/graph-api-response.png" alt-text="Screenshot of testing the Graph API in the portal."::: |
| 162 | + |
| 163 | + A successful response returns user data from the Microsoft Graph. |
| 164 | + |
| 165 | +## Next steps |
| 166 | + |
| 167 | +* Learn more about [access restriction policies](api-management-access-restriction-policies.md) |
| 168 | +* Learn more about [scopes and permissions](../active-directory/develop/scopes-oidc.md) in Azure AD. |
0 commit comments