azure-api-managementazure-ad-graph-api

how to integrate graph api in apim service


I have an Azure Application name and I can access the enterprise application properties with the help of graph Api.

I want to integrate the below graph Api into my APIM service for fetching the properties. Is there any reference for creating a mock Api in Api management service?

Microsoft Graph:

GET https://graph.microsoft.com/v1.0/servicePrincipals/?$filter=displayName eq 'Demo App' &$select=appId,appDisplayName

can i use the policies "rewrite-uri" for generating token and then pass it to the "send-request".

or is there any simple way available for integration?


Solution

  • Initially, I registered one application and granted Application.Read.All Microsoft Graph permission of Application type with admin consent as below:

    enter image description here

    Make sure to add redirect URI as "https://authorization-manager.consent.azure-apim.net/redirect/apim/YOUR-APIM-SERVICENAME" in Web platform of application as below:

    enter image description here

    Now, create credential provider in your APIM service with Identity provider as Azure Active Directory v1, grant type as client credentials and resource URL as https://graph.microsoft.com like this:

    enter image description here

    Under Connection tab, enter client ID and client secret values of your app registration with connection name:

    enter image description here

    You can confirm it by checking the status of connection, visiting newly created credential provider like this:

    enter image description here

    Create new HTTP API with "https://graph.microsoft.com/v1.0" as Web service URL:

    enter image description here

    Create GET operation in it with URL as /servicePrincipals :

    enter image description here

    Make use of below sample policy file code that generates token and use it to call Graph API:

    <policies>
        <inbound>
            <base />
            <get-authorization-context provider-id="graphcred01" authorization-id="graphconnection" context-variable-name="auth-context" identity-type="managed" ignore-error="false" />
            <set-header name="Authorization" exists-action="override">
                <value>@("Bearer " + ((Authorization)context.Variables.GetValueOrDefault("auth-context"))?.AccessToken)</value>
            </set-header>
            <!-- Rewrite URI to include filter and select parameters, with properly escaped characters -->
            <rewrite-uri template="/servicePrincipals/?$filter=displayName eq &apos;DemoApp&apos;&amp;$select=appId,appDisplayName" />
        </inbound>
        <backend>
            <base />
        </backend>
        <outbound>
            <base />
        </outbound>
        <on-error>
            <base />
        </on-error>
    </policies>
    

    Response:

    enter image description here

    Reference:

    Create connection to Microsoft Graph API - Azure API Management | Microsoft