Call Microsoft Graph API  with application permission from Power Automate using HTTP connector
Published: 2021-03-22 09:40:54

In this post, I will be showing you the steps with instructions to call Microsoft graph Endpoints as a daemon app using Application permissions with the help of HTTP connector. Calling graph from a flow opens a wide range of possibilities which are not available with the prebuilt connectors. As of now you will not be able to call Microsoft graph with application permissions using a custom connector.

## Pre-Requisites:

- Access to HTTP Premium Connector in Power Automate
- Access to register Azure AD Application in Azure AD Portal
Application Registration in Azure AD Portal:
Register an application in Azure AD and obtain the client id, client secret & tenant id for the registered application. In this example I have added the Application permission Calendars.Read to access all the recent events of a user from Outlook.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eyqkfqf1b8q8zzvdpv98.png)

It is not required in the Azure AD application to have a redirect URI.

# Power Automate Flow:
It is now time to generate the graph token using the HTTP connector in flow which is a pre-requisite to call the Graph API endpoint. The only authentication flow to generate a access token for application permissions is Client credentials.

### To generate a token

Store the Client Secret on a String variable
Make a HTTP request using the HTTP connector with the following details. Make sure to replace the string for tenantId, azureAdAppclientId and azureAdAppclientSecret
# Method 1:
Add a HTTP connector action to the flow for making a POST request per the following information

**HTTP Method**: POST

**URI**: https://login.microsoftonline.com/yourtenantId/oauth2/v2.0/token

**Headers**: Content-Type: application/x-www-form-urlencoded

**Body**:

Replace the tenantId, client id and client secret from the variable

tenant=yourtenantId&client_id=azureAdAppclientId&client_secret=@{decodeUriComponent(variables('azureAdAppclientSecret'))}&grant_type=client_credentials&scope=https://graph.microsoft.com/.default
For the client secret make sure to URL encode using the expression **encodeUriComponent(variables(‘clientSecret’))** else the request will fail due to the presence of special characters.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/22cyhuagz61x0g9fgzlz.png)
To extract the token from the above request, add the parse JSON action with Content from the HTTP request body and the following schema
```{
    "type": "object",
    "properties": {
        "token_type": {
            "type": "string"
        },
        "scope": {
            "type": "string"
        },
        "expires_in": {
            "type": "integer"
        },
        "ext_expires_in": {
            "type": "integer"
        },
        "access_token": {
            "type": "string"
        },
        "refresh_token": {
            "type": "string"
        }
    }
}```

Add the Body from the dynamic content from the HTTP – GET Token action to the content of the Parse JSON action
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2341awhmpjjwyqu7hc8.png)
Include the access token from the Output of the Parse JSON action when calling the Microsoft Graph API on the Headers sections as shown below

To get the users events from the default calendar

https://graph.microsoft.com/v1.0/users/{id | userPrincipalName}/calendar/events
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f7lx82f5vxbod06hg2lc.png)
# Method 2:
You can also make a request to Graph API using the Active Directory OAuth Authentication under the advanced options of the action as shown below
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jsu3pmweoux4xhgjeu95.png)
My other blog post to call [Microsoft graph API in Power Apps and Power Automate using a custom connector](https://ashiqf.com/2021/03/16/call-microsoft-graph-api-in-power-apps-and-power-automate-using-a-custom-connector/)


**Summary**: I have written a blog to get the attendee details of a meeting using this approach to Microsoft graph event endpoint API. Hope you have found this informational & thanks for reading. If you are visiting my blog for the first time, please do take a look at my other Microsoft graph in Power Automate blogposts in my  [blog site](https://ashiqf.com).