Integrate Python to connect to Dynamics 365/ CDS Web API using Client credentials

Well this is my third post on Python and I am loving the language and it’s simple syntax of doing things.

So I decided to explore on how to connect to Dynamics 365 WebAPI using Python. And I came across this wonderful post – Highly recommended you go through this one.

While implementing the same, I stumbled upon few areas which I will help to resolve. Also the blog demonstrates with grant_type=password. I will show with grant_type=clientcredentials.

The first thing is obviously register an app with Azure AD and get the client_id and client_secret. And also make sure that the app have permissions to access the CDS environments. If you are unware of these, there are hundreds of blogs to guide you through this. For the brevity of this blog, I am skipping this one.

let’s jump to the code now. I am using Sublime Text 3 editor for this. However you can use any python editor of your choice.


If you really look at the code, you can draw a sharp analogy to C#. I am basically importing the JSON and requests library to work with web requests and parsing the json response. The rest of the code is explanatory. In this code I am just writing the code to get the access token. In the next blog I am going to write on how to fetch data using the token. But at the moment, I leave that you.

I build and run the code. But I get the error – “module requests not found”. This is more of C# build error where it says some dll is missing. Just like we need to install the package, so do we need to do the same here as well. But if you are new to python like me, even the simplest of things take time.

To install the requests module, open the command prompt in your machine and go to scripts folder of python installation library. Then use the command “easy_install requests”.


And the requests package shall be installed successfully.

Now I build and run and I can see the access token being printed on the console. Delight for starters like me.


Hope you found this interesting. You can follow my blog for more posts like this.

Debajit Dutta

(Dynamics MVP)

For consultation/ corporate training visit or reach out to us at

Our product offerings:

CRM-Sharepoint Attachment uploader and metadata manager (

Notes Manager (

Role based views for Dynamics 365 (

Record Cloner for Dynamics 365 (

Leave a Reply