Business Central OAuth2 for Power Automate 🛂

Generally when writing a post I quickly explain the scenario and get straight to the solution. I need to break that rule here to avoid confusion. As the title suggests this post is about using OAuth2 with Power Automate – applies also to Azure Logic Apps. However, if you have Power Automate flows using the BC connector you are already covered. If you have custom connectors for BC data which use OAuth2 for authentication. You are also covered. So what’s the point of this blog you ask? If you use the HTTP connector in your flows to interact with BC data – you have opened the right blog post 👍🏼. Why does this post matter? Well at the date of writing, 01/03/22, basic authentication for BC web services is being deprecated (https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/upgrade/deprecated-features-platform). OAuth2 is the replacement. If you can move away from using the HTTP connector then please do. That way you can just natively pickup the correct authentication. I say there are two main reasons you wouldn’t move things to the BC connector. If you have published a codeunit as a web service or you use query objects (which aren’t the API type). Lesser reasons might be that you don’t have development resource or want to avoid cost of refactoring. After all Power BI get to use legacy web services so Power Automate should get to as well right? For PowerApps you need to use a custom connector or the BC connector. You might have Power Automate flows which run from PowerApps. In which case if they use the HTTP connector, same explanation as above. Woah! 😮 that was a wordy intro. If you’re still with me let’s get to the good bit…

I will admit early on I only came across my answer thanks to some brilliant blogs! I highly recommend reviewing Arend Jan’s posts in particular if you have little background on working/setting up OAuth2 for BC. Links:

The solution is to have a standalone flow which will retrieve the access token. You will use this flow as a child to you main PA flow. This pattern will work well as you don’t have to alter your existing PA flows using the HTTP connector that much. Let’s get the child flow out the way first as that’s the longer part.

Here is the text string from the compose action mentioned in the gallery of images. Replace the your_client_ID and your_client_secret parts to form the correct string:

concat('grant_type=',encodeUriComponent('client_credentials'),'&scope=',encodeUriComponent('https://api.businesscentral.dynamics.com/.default'),'&client_id=',encodeUriComponent('your_client_ID'),'&client_secret=',encodeUriComponent('your_client_secret'))

Now we have the child flow up and running let’s plumb it in to a parent flow 🧑‍🔧 I will go through each step with images again.

It’s a fairly elegant way to slot in what is needed for the PA flows this might apply to. There are a number of my own previous blog posts which need this solution – like this one: https://joshanglesea.wordpress.com/2020/10/19/business-central-month-end/

To make life easy I’ve created a template of the child flow which you could import into your environment. There are setups that need dealing with before it will work though: https://github.com/JAng13sea/Blogs/tree/master/Template%20Get%20Access%20Token%20PA%20Flow

If you read this because you want to work with web services to get data. You might like this post too: https://joshanglesea.wordpress.com/2023/03/09/business-central-generic-api/

8 thoughts on “Business Central OAuth2 for Power Automate 🛂

  1. Thanks Josh for this great article. I just have a question. For UN and PW you mentioned that user can user previous step to get user name and password. Can you please share an example? I mean if I enter my user name and password in those fields then I cannot share the flow since everyone else can see them. Thanks

    Like

    1. The child flow will need to be altered. The trigger for the child flow in my original example uses a GET method. Instead you would need to change that to be a POST method so that you can send a request body. This is an example JSON schema:
      {
      “type”: “object”,
      “properties”: {
      “UN”: {
      “type”: “string”
      },
      “PW”: {
      “type”: “string”
      }
      }
      }
      When calling the child flow you can then pass in the data of your choice. You can then treat that data like parameters and load them into the variables or straight into the request to AAD. In my example I have used an account which is more like a service account.

      Like

      1. Thanks Josh. This is really helpful

        I find it very difficult to post dimension into BC using HTTP. Looks like API V1 support dimension but no other fields and API V2 does not support dimension. Only below API support them all but when I use HTTP this API does not capture last Line No and I need to manually add that. I saw in BC forum that you used HTTP before but looks like you have used custom API. is this accurate?

        https://api.businesscentral.dynamics.com/v2.0/tenantID/Environment Name/ODataV4/Company(Company Name)/workflowGenJournalLines

        Like

      2. The custom API was necessary yes. Microsoft are aware and it will be resolved in the future. For now whatever workaround you can accept is the temporary solution

        Like

Leave a comment