When I started writing about OAuth changes, I thought I would be able to cover things within 4 blog posts, but it looks like I might need a few more extra. Not going to go back and change the previous blog posts about how many blog posts I’m going to write. 😊
If you have Microsoft Dynamics 365 Business Central APIs that create scheduled tasks upon API requests, it might be a good idea to revisit them before updating the current Basic authentication mechanism to recommend Service to-Service authentication with OAuth 2.0.
If you do not revisit and implement new ways to handle the scheduled task, you will run into the below error when you start testing.
You do not have permission to create or run scheduled tasks.
We ran into it a few months ago with one of our clients and luckily it was during the UAT phase of the OAuth change.
Is this a bug or something we should inform Microsoft about? Because the API was working fine all this time and as soon as we implement OAuth 2.0, API starts to throw this error? No, this is by design.
In Basic authentication, we used an actual user to connect to BC, but with the new S2S authentication we are not using an actual user and Scheduled tasks must be created and executed in the context of a licensed user. Therefore API requests cannot schedule a background task. It is simple as that.
So now what? What if you really need to create scheduled tasks with the API requests? What we came up with is a simple solution. Instead of creating a scheduled task on the fly, we insert a record to a new table we created called “API Request Queue”. Then we have a Job Queue running every 5 min to check if there are any new API requests on the “API Request Queue” table, if so then Job Queue will take that request and create a new scheduled task. Job Queue was running under a fully licensed user, giving necessary permission to create the scheduled tasks.
Hope it helped. Good luck with the changes and if you have any questions comment or send me a direct message on Twitter.
Thank you and Regards,
Tharanga Chandrasekara