DEV Community

Jingyi Z.
Jingyi Z.

Posted on

Call Microsoft Graph API in Power Automate using custom connector

Welcome

Credits- http://m.alnafsy.com/

Now that we are well acquainted with power apps and creating a microsoft graph custom connector in it, let us familiarize ourselves with the following terms once so that we can proceed to call Microsoft graph API in Power Automate using custome connectors for which we will require to set the criteria in authentication.

Azure API Management Gateway

Custom connectors are supported by Microsoft Azure API Management infrastructure. When a connection to the underlying API is created, the API Management gateway stores the API credentials or tokens, depending on the type of authentication used, on a per-connection basis in a token store. This solution enables authentication at the connection level.

Key

Credits- https://commons.wikimedia.org/wiki/File:Crypto_key.svg

Azure API Authentication

Before using any connector in Azure Logic Apps, Power Automate, or Power Apps, the user needs to create a connection by authenticating to the network service. There are different types of authentication.

  1. No authentication- No further information is required.
  2. Basic authentication- The user will provide the user name and password to create the connection.
  3. API Key- Used by web services. Make sure that you define- Parameter label, Parameter name, Parameter location
  4. OAuth 2.0 authentication- Available for online connectors. Provides implementations for specific services and prebuilt identity provider templates, when selected, fill in many of the fields that are required by OAuth 2.0.
  5. Windows authentication- Available only for connections that use on-premises data gateway, when the Connect via on-premises data gateway check box is set on the General tab. When a new connection is created, the user will need to provide Windows credentials for the service and then select one of the installed on-premises gateways.

Azure AD Authentication

Steps

Prerequisites

An account on Power Apps Portal, Graph Explorer, Microsoft Azure.

Let's get started!

Step 1: Create a new solution.

  1. Go to Power Apps maker portal and make sure to be in the correct environment.
  2. Select Solutions > + New solution and enter the following details.
  3. Don't navigate away from this page after selecting 'Create'.

img2

Step 2: Use Graph Explorer to test the API.

  1. Sign in to the Graph Explorer and use it to test the API. Read the permissions and continue if you agree.
  2. Make sure that GET is selected for the verb, add /insights/used to the URL, and then select Run query.
  3. You should get a 403 error indicating that Graph Explorer lacks your permission to perform this action.
  4. Select the Modify permissions tab to grant Graph Explorer permission > Select Sites.Read.All > Consent.
  5. Read the requested permissions, continue if you agree and select Run query again.

img3

  1. Start a new browser session tab. Sign in to OneDrive Personal Cloud Storage.
  2. Select + New and select Word document.

igm4

  1. Enter some test text in the Word file. The document will be saved automatically for you.
  2. Go back to the Graph Explorer and run the same query again.
  3. You should now get a response with values. Select the response JSON, right-click, select Copy, and then save it.

img5

Step 3: Register a new application and add permissions.

  1. Sign in to Microsoft Azure with your user admin credentials.
  2. Select Show portal menu and then select Azure Active Directory.

img6

  1. Enter Learn last used connector for Name, enter https://global.consent.azure-apim.net/redirect for Redirect URI, and then select Register.

img9

  1. Select API permissions and then select + Add a permission.

img8

  1. Select Certificates & secrets and + New client secret.
  2. Enter Last used connector action for Description, select In 1 year for Expires, and then select Add.
  3. Copy the Value and save it for later because it won't be shown again. You will use this user secret when creating the connector.
  4. Select Overview, copy the Application (Client) ID and save it on a notepad. You'll use this client ID when creating the connector.

img12

Step 4: Create a custom connector.

  1. Make sure to sign in and be in the right environment in the Power Apps Admin Portal.
  2. Select Solutions and then select to open the Contoso graph solution that you created in 'Step 1: Create a new solution'.
  3. Select + New > Other > Custom connector.
  4. Enter Contoso graph for Connector name, scroll down, enter graph.microsoft.com for Host and /v1.0 for Base URL and select Security.

img13

  1. Select OAuth 2.0 for Authentication and Azure Active Directory for Identity Provider.
  2. Paste the ID that you copied from Azure in the Client id field and then paste the Value that you copied from Azure in the Client secret field.
  3. Enter https://graph.microsoft.com for Resource URL and then select Create connector. Don't navigate away from the page.

img14

Step 5: Add the action.

  1. Select Definition > + New action.
  2. Enter Last used for Summary and LastUsed for Operation ID. Go to the Request section and select + Import from sample.
  3. Select Get for the verb, enter /me/insights/used for URL, and then select Import. Scroll down and select the default response.
  4. Select + Import from sample. Paste the response that you copied from Graph Explorer in the Body field and then select Import.

img15

  1. Select Update connector. Don't navigate away from this page.

Step 6: Test the connector.

  1. Select the Test tab and then select + New connection.
  2. Provide your credentials. Read the requested permissions and continue.
  3. Select Refresh connections and Test operation. You should see a 200 status, and the response should look like the image.

response-status

Summary

Congratulations! You have now created an environment to call Microsoft Graph API in Power Automate and can proceed to learn more in this area. Do leave your feedback in the comments below. Our next blog will on how to connect Power BI to Microsoft Graph. Stay tuned!

Next Steps

More info on overview of Microsoft Graph

Basics of Power BI

Blog 5: Connecting Power BI to Microsoft Graph

Top comments (0)