Obtaining D365 Connection Token via HTTP in Power Automate

In a previous article, we discussed the registration of application users. Today, we will use the HTTP step in Power Automate to obtain a token based on the application user information for querying D365 OData WebAPI data.Adding Application User in D365Obtaining D365 Connection Token via HTTP in Power AutomateCreate a new instant cloud flow and select the manual trigger flow.Obtaining D365 Connection Token via HTTP in Power AutomateAdd [New Step]Obtaining D365 Connection Token via HTTP in Power AutomateSelect the HTTP stepObtaining D365 Connection Token via HTTP in Power AutomateSelect [HTTP]Obtaining D365 Connection Token via HTTP in Power AutomateInput the following content:1. Select POST method for requesting the token.2. The token request URL:https://login.microsoftonline.com/{Tenant ID}/oauth2/tokenThe tenant ID can be found in the application user settings as follows:Obtaining D365 Connection Token via HTTP in Power Automate3. In the header, add the value of Content-Type as application/x-www-form-urlencoded. This setting is used to parse the body content in JSON format.Note: It must be set to this type; otherwise, the body cannot be parsed.4. Body:

grant_type=client_credentials&client_id=b1e1dcd7-d362-4a0a-b933-8f5313f167cc&client_secret=VGo8Q~JHwQh45oHhoplOUgUUFmWx20WSbaWg&resource=https://orgfe71e4a7.crm5.dynamics.com

Where:grant_type should be set to client_credentials, a fixed value indicating that authentication is performed via client mode.resource is the URL of the D365 you want to connect to.client_id is the application (client) ID as follows:Obtaining D365 Connection Token via HTTP in Power Automateclient_secret is the application user client secret as follows:Obtaining D365 Connection Token via HTTP in Power AutomateAfter filling in, we will run the flow to test it.Obtaining D365 Connection Token via HTTP in Power AutomateThe test ran successfully.Next, add a step to parse the returned JSON data to extract the token value.Obtaining D365 Connection Token via HTTP in Power AutomateSearch for [Data] and then select [Data Operations]Obtaining D365 Connection Token via HTTP in Power AutomateSelect [Parse JSON]Obtaining D365 Connection Token via HTTP in Power AutomateContent: Select the [Body] information from the previous step.Obtaining D365 Connection Token via HTTP in Power AutomateSchema: Parse the structure of the JSON data, and in the following steps, you can directly select the property values of the JSON data structure.Click [Generate from Sample] as shown below.Obtaining D365 Connection Token via HTTP in Power AutomateAdd the body JSON format data returned from the previous step’s HTTP token acquisition step, and it will automatically parse the JSON schema information.Obtaining D365 Connection Token via HTTP in Power AutomateThis way, you can perform JSON data parsing.Obtaining D365 Connection Token via HTTP in Power AutomateRun the test.Obtaining D365 Connection Token via HTTP in Power AutomateThe test passed normally. Next, add the [Edit] step in [Data Operations] to store the parsed token data.Obtaining D365 Connection Token via HTTP in Power AutomateObtaining D365 Connection Token via HTTP in Power AutomateThe property access_token parsed from the previous step’s JSON can be selected directly.Obtaining D365 Connection Token via HTTP in Power AutomateThen add the HTTP request step with the token to retrieve D365 data.Obtaining D365 Connection Token via HTTP in Power AutomateObtaining D365 Connection Token via HTTP in Power AutomateInput the following content:URI:https://orgfe71e4a7.crm5.dynamics.com/api/data/v9.2/new_testones?$select=new_name, this is the D365 OData API query statement.Header:Authorization: Pass the token obtained from the previous step, prefixed with Bearer. Note that there should be a space after Bearer, followed by the token parsed from the previous step.Example: Bearer asashsakdhjk121jkahsajskajshka……….OData-MaxVersion: 4.0 (fixed value)OData-Version: 4.0 (fixed value)Content-Type: Set to application/jsonRun the test.Obtaining D365 Connection Token via HTTP in Power AutomateObtaining D365 Connection Token via HTTP in Power Automate

Leave a Comment