February 25, 2021

Call SharePoint Rest API with Client ID and Client Secret from Power Automate

Overview:

We recently implemented an automated approval process in SharePoint Online with Power Automate for a construction and engineering company based out of Washington, United States. We came across a scenario where we required to consume SharePoint data in Power Automate with REST API using Client ID & Client Secret. Here in this article, we will discuss step by step process to implement such functionality.


Prerequisite:

We must have SharePoint App registered, please visit this article for step by step process to register the SharePoint App.

Copy Tenant ID from Azure Portal:

  1. Open Azure Portal. From Left Navigation click on Azure Active Directory
  2. Here, you will get your Tenant ID.

Steps to call SharePoint REST APIs with Client ID and Client Secret from Power Automate (MS Flow)

  1. Initialize variables for – Client ID, Client Secret, Tenant Name and Tenant ID.
    1. Client ID and Client Secret = We can get these from where we registered the App.
    2. Tenant Name = Name of the Tenant.
    3. Tenant ID = We can get that from Azure Portal (Step 2 from above section).
  2. Prepare Body Variable.
     grant_type=client_credentials&client_id=<<Client ID>>@<<TenantID>>&client_secret=<<Client Secret>>&resource=00000003-0000-0ff1-ce00-000000000000/<<TenantName>>.sharepoint.com@<<TenantID>>   
    
  3. Now, add “HTTP action”.
    1. Method: POST
    2. URI: https://accounts.accesscontrol.windows.net/@{variables('tenantID')}/tokens/OAuth/2
    3. Headers:  Content-Type: application/x-www-form-urlencoded
    4. Body: Pass the variable defined in Step 2.
  4. Get Access token in Compose Variable.
    Expression: body('Get_Access_Token_from_SharePoint')['access_token']
  5. Add HTTP Action to get users from SharePoint group.
    1. Method: Get
    2. URI: https://tenantname.sharepoint.com/sites/sitename/_api/web/sitegroups/getbyname('groupname')/users
    3. Headers: 
    4. Accept: application/json;odata=verbose
      Authorization: Bearer @{outputs('Compose')}
  6. Then, let’s Parse our JSON using Select Statement.

This is how we can create a flow to find the users from SharePoint Group with client Id and client secret. 


Test Flow

The flow ran successfully.
We got the following access token using first API call.
Then this access token used to call the group users. We got the following users.


Conclusion:

This is how, we can call SharePoint Rest API with Client ID and Client Secret from Power Automate. Isn’t that amazing? 

If you have any questions you can reach out our SharePoint Consulting team here.

No comments:

Post a Comment