{"id":150,"date":"2022-02-04T18:35:37","date_gmt":"2022-02-04T17:35:37","guid":{"rendered":"https:\/\/boudewijnvandendungen.azurewebsites.net\/?p=150"},"modified":"2022-04-05T09:59:37","modified_gmt":"2022-04-05T07:59:37","slug":"calling-the-microsoft-dynamics-365-web-api-with-postman","status":"publish","type":"post","link":"https:\/\/boudewijnvandendungen.com\/calling-the-microsoft-dynamics-365-web-api-with-postman\/","title":{"rendered":"Calling the Microsoft Dynamics 365 Web API with Postman"},"content":{"rendered":"\n

A new Mule 4.x project I am working on will be getting its Customer relationship management data in Odata<\/em> format from Microsoft Dynamics using the Dynamics 365 Web API. Seeing as test data was made available, I wanted to call the API and see what the response would be. For these types of calls I generally use Postman<\/a>. <\/p>\n\n\n\n

However, after filling in the Request URL and the Authorization, I kept getting a ‘401 Unauthorized’<\/em> response. <\/p>\n\n\n\n

Postman returns a 401 Unauthorized When trying to call the Dynamics 365 Web API<\/strong><\/h2>\n\n\n\n

After some searching I eventually contacted our CRM partner for support, where we reproduced the error despite following official documentation<\/a>. We managed to find out that the API was working – security settings were fine – just not when using Postman. <\/p>\n\n\n\n

Giving it some thought later in the day, I went and tried a more basic approach to requesting the Oauth2<\/em> token – calling the \/oauth2\/token<\/em> directly.<\/p>\n\n\n\n

Retrieving the Oauth2 token<\/strong><\/h3>\n\n\n\n

If you’ve never used Oauth2<\/em>, don’t worry. Getting an access token is fairly straightforward.<\/p>\n\n\n\n

In Postman, open a new request.<\/p>\n\n\n\n

Change the Method to [POST], and use the following link for the request URL: https:\/\/login.microsoftonline.com\/<tenant_id>\/oauth2\/token.<\/em>
Replace <tenant_id><\/em> with your own organization uuid.<\/p>\n\n\n\n

Next, create the following parameters in the body using ‘x-www-form-urlencoded’<\/em>:
‘grant_type’<\/em> : ‘client_credentials’<\/em>
‘client_id’<\/em> : use your client id here
‘client_secret’<\/em> : use your client secret here
resource’<\/em> : use your resource<\/em>, will probably look like org.crm.dynamics.com<\/em> (do not add \/api\/data\/v9.x<\/em>).<\/p>\n\n\n\n

You can set these parameters and URL details in an environment, but there is a chance variables aren’t being passed correctly and Postman (or more accurately Dynamics 365) might throw an error – Make sure you fill in both the ‘Initial Value’<\/em> and the ‘Current Value’<\/em> as this seems to be required. The documentation I linked above will have a page on Postman environments in case you want to try.<\/p>\n\n\n\n

The ‘tenant_id’<\/em> is used to request and receive a token related to your specific organization, and you validate your identity with the ‘client_id’<\/em> and ‘client_secret’<\/em>. The token is only valid for calling the specific ‘resource’<\/em>, so if you wanted to use this newly acquired token to call a different resource within your tenant, it wouldn’t work.<\/p>\n\n\n\n

\"\"<\/figure><\/div>\n\n\n\n

As you can see you will get a standard Bearer token in the response. Copy the ‘access_token’<\/em>. We will use it in the next step.<\/p>\n\n\n\n

Calling the resource<\/h2>\n\n\n\n

Now that we have an Oauth2<\/em> token, we can call the resource like in the original attempt and get the CRM data.<\/p>\n\n\n\n

In Postman, open a new request.
Set the method to [GET], and fill in the ‘resource’<\/em> as request URL. Append it with \/api\/data\/v9.x<\/em> – depending on which version you are using, common used are 9.0 or 9.1, then click on the Authorization tab. Select Oauth2<\/em> as type.<\/p>\n\n\n\n

\"\"<\/figure><\/div>\n\n\n\n

Paste your copied ‘access_token’<\/em> into the blank area in order to add it to the request. You don’t have to change any of the other settings.<\/p>\n\n\n\n

That’s it! from now on you can connect with the Dynamics 365 Web API and GET the information stored in the resource.<\/p>\n\n\n\n

Solution for the original issue<\/h2>\n\n\n\n

Updating my Postman application to the latest version gave me more options to configure the token, adding the resource into the advanced options solved the issue I had at the beginning and will allow you to get the token through regular configurations in the authorization tab. <\/p>\n","protected":false},"excerpt":{"rendered":"

A new Mule 4.x project I am working on will be getting its Customer relationship management data in Odata format from Microsoft Dynamics using the Dynamics 365 Web API. Seeing as test data was made available, I wanted to call the API and see what the response would be. For these types of calls I … Continue reading “Calling the Microsoft Dynamics 365 Web API with Postman”<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[18,2],"tags":[],"_links":{"self":[{"href":"https:\/\/boudewijnvandendungen.com\/wp-json\/wp\/v2\/posts\/150"}],"collection":[{"href":"https:\/\/boudewijnvandendungen.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/boudewijnvandendungen.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/boudewijnvandendungen.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/boudewijnvandendungen.com\/wp-json\/wp\/v2\/comments?post=150"}],"version-history":[{"count":4,"href":"https:\/\/boudewijnvandendungen.com\/wp-json\/wp\/v2\/posts\/150\/revisions"}],"predecessor-version":[{"id":191,"href":"https:\/\/boudewijnvandendungen.com\/wp-json\/wp\/v2\/posts\/150\/revisions\/191"}],"wp:attachment":[{"href":"https:\/\/boudewijnvandendungen.com\/wp-json\/wp\/v2\/media?parent=150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/boudewijnvandendungen.com\/wp-json\/wp\/v2\/categories?post=150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/boudewijnvandendungen.com\/wp-json\/wp\/v2\/tags?post=150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}