Authenticate Graph API Using Power Automate - Part 2
In my previous post, we created an application in azure active directory and gave Graph API access to this application. If you are not familiar with this area, I highly recommend that you read -How to set up application access to graph API in azure AD,
Now that we have set up application access in azure AD, we can now use this application to authenticate Graph API via power automate.
Like all my flows, we can break this down to two stages:
1. Access token request
2. Parse response and retrieve the bearer token
Create a new flow (Instant from blank)
Add 3 inputs to the flow trigger:
1. Organization/tenant ID : Text Input
2. AppID/Client ID: Text Input
3. Client Secret: Text Input
Now, let's create the "HTTP Request" flow action:
Here are the parameters
Method : POST
URI : https://login.microsoftonline.com/{Add Organization}/oauth2/token
Content-Type : application/x-www-form-urlencoded
Body :
client_id= {AppID}
&scope= https://graph.microsoft.com/.default
&client_secret= {ClientSecret}
&grant_type=client_credentials
Now, let's run this flow - enter below values in the trigger fields and submit.
Once successfully completed, we can see the access_token value in the response.
Here are the steps:
1. Copy the response body to a notepad
2. Add JSON Parse action to the flow
3. As "Content", select the response body from dynamic content panel
4. Click "Generate from sample" and copy the response body from notepad
5. Click "Done"
Now, we need to store the access token. For this, let's create a string variable "AccessToken"
For the value, we can select "access_token" from Parse JSON action.
All set! Let's test the flow now.
You will now see the access token value in the "Value" field of "Initialize variable" action.
This token can be used to call Graph API methods. Depending on the requirement, now you can add more steps that would call Graph API and perform actions.
Here is the Graph API documentation : https://docs.microsoft.com/en-us/graph/
Explore the methods in this documentation and see how you can incorporate these API calls in power automate.
With few modifications to this flow, we can even store the credentials (Client Secret, App ID etc) in a separate location (Config file) without having to enter during run time.
Now, it's up to you to design complex scenarios by adding subsequent steps as needed. Just play around with it and you will figure out the best way to implement Graph API calls within your flow.
Happy Learning!
Now that we have set up application access in azure AD, we can now use this application to authenticate Graph API via power automate.
Like all my flows, we can break this down to two stages:
1. Access token request
2. Parse response and retrieve the bearer token
Access token request(Flow)
Here are the Steps:Create a new flow (Instant from blank)
Add 3 inputs to the flow trigger:
1. Organization/tenant ID : Text Input
2. AppID/Client ID: Text Input
3. Client Secret: Text Input
Now, let's create the "HTTP Request" flow action:
Here are the parameters
Method : POST
URI : https://login.microsoftonline.com/{Add Organization}/oauth2/token
Content-Type : application/x-www-form-urlencoded
Body :
client_id= {AppID}
&scope= https://graph.microsoft.com/.default
&client_secret= {ClientSecret}
&grant_type=client_credentials
Now, let's run this flow - enter below values in the trigger fields and submit.
- AppID - Application (client) ID from Azure AD application
- Client Secret : Client Secret of the app
- Organization : Directory (tenant) ID from azure AD application
Parse Response and get Access Token
We can parse the response and get token value simply by using "JSON Parse" action.Here are the steps:
1. Copy the response body to a notepad
2. Add JSON Parse action to the flow
3. As "Content", select the response body from dynamic content panel
4. Click "Generate from sample" and copy the response body from notepad
5. Click "Done"
Now, we need to store the access token. For this, let's create a string variable "AccessToken"
For the value, we can select "access_token" from Parse JSON action.
All set! Let's test the flow now.
You will now see the access token value in the "Value" field of "Initialize variable" action.
This token can be used to call Graph API methods. Depending on the requirement, now you can add more steps that would call Graph API and perform actions.
Here is the Graph API documentation : https://docs.microsoft.com/en-us/graph/
Explore the methods in this documentation and see how you can incorporate these API calls in power automate.
With few modifications to this flow, we can even store the credentials (Client Secret, App ID etc) in a separate location (Config file) without having to enter during run time.
Now, it's up to you to design complex scenarios by adding subsequent steps as needed. Just play around with it and you will figure out the best way to implement Graph API calls within your flow.
Happy Learning!
Comments
Post a Comment