If you are writing an integration into D365 you may want to setup Postman to call D365 data entities. This will allow you test the data entities and see what data your receiving application will retrieve without waiting to ask the receiving application to make the call.
The last article, we completed the setup steps needed to allow Postman to make the call. We also wrote down the pieces of information we will need to setup Postman. In this article we will setup Postman to call D365 Data Entities.
Before we actually make the call to the data entity. We will need to do two things first:
- Create Get Token Request in Postman. This will get an access_token needed to access D365.
- Store the Access Token In An Environment Variable.
What You Will Need
In order to use Postman you will need the following pieces.
- Download and install Postman. https://www.postman.com/downloads/
- The Directory ID, found in the last article.
- The Client ID, found in the last article.
- The Client Secret ID, found in the last article.
- The URL of the D365 environment you wish to connect to.
Create Get Token Request In Postman
Open Postman.
Click the ‘New’ button in the top right corner. Select ‘Request’.
In the Dialog that open, enter in a Request name. Then click Save.
In the main window, change the request type to be ‘Post’.
Then enter in the URL to be the following. Replace <Directory ID> to be your Directory ID found in the Azure Portal.
https://login.microsoftonline.com/<Directory ID>/oauth2/v2.0/token
Next, select the ‘Headers’ tab and specify the following values. Most should be defaulted in. However you will need to set the Host to login.microsoftonline.com.
Click on the Body tab and specify the following Key Value pairs.
- The client_Id should be the Client ID found in Azure Portal.
- The client_secret should be the Client Secret ID found in the Azure Portal.
- The grant_type should be the text ‘client_credentials‘.
- The Scope should be set to the URL of your D365 instance that you wish to connect to, followed by the text ‘/.default‘
https://<D365 URL>/.default
Click the ‘Save’ button in the top right to save the request.
Click the ‘Send’ button. If you have setup the request correctly, you will see something similar in the response body. The access_token value will contain a long string. And the Status will be 200 OK.
Store the Access Token In An Environment Variable
We will need to use the access_token value from the last step, whenever we make a call to D365. This token expires in about an hour. While we could regularly copy and paste the long string into our request, there is an easier way. We can have postman store it in an environment variable.
In Postman, click the New button. Then select ‘Environment’.
In the dialog that opens specify a name for the environment. In the first line, enter the text ‘access_token’ under the variable column. Then click the Add button.
Now go back and select the ‘Get Token’ request.
Select the ‘Tests’ tab.
Enter the following code. This takes the access_token value in the response of this request, and stores it in the environment variable named ‘access_token’.
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("access_token", jsonData.access_token);
Push ‘Save’.
Next, in the top right corner, select the name of your environment from the drop down.
Then click ‘Send’ on the ‘Get Token’ request. This will cause the access_token in the response to be stored in the environment variable named ‘access_token’.
Setup Postman Request To D365 Data Entity
Finally, we can make a request to the D365 Data entity.
In Postman, click the ‘New’ button. Then select ‘Request’.
Set the request type to ‘Get’.
Specify the following text in the URL field. Replace <D365 URL> with the URL you use to access the D365 environment.
https://<D365 URL>/data/CustomersV2
In the Headers tab, specify the following Key Value Pairs.
Most of the pairs will default in. However, you will need to set the Authorization value to the text ‘Bearer {{access_token}}’. This tells Postman to replace the text ‘access_token’ with the value in the access_token environment variable.
If you do not setup an environment variable, manually copy and paste the long access_token value from the first call into this field. Note, keep the ‘Bearer {{ }}’ text.
Click ‘Save’
Click ‘Send’
If you set things up correctly, you will get a response with a Status of 200 Ok. And you will see customer data returned as JSON text in the response.
Conclusion
Congratulations! You have successfully setup Postman to call D365 Data Entities. In this case we called the CustomersV2 data entity. Change the URL to query other data entities. Adding the filter parameter to the URL further will allow you to filter the results being returned.
Wow, this article is nice, my sister is analyzing
such things, so I am going to inform her.
Great post! We will be linking to this great pokst on ourr
website. Keep up the great writing.
I am trying to hit CustomersV3 through post method and i followed same procedure as you mention, but i am getting error.
“Message”: “Authentication failed.”,
“StackTrace”: null,
“ExceptionType”: “System.InvalidOperationException”
I tried all possibilities even though not able resolve the issue. Would you advise ?
I find it much better to get the token in a pre-request script on the collection. This then saves the value of the token in each environment before every call (if the token has expired). Saves a lot of 401 errors
Yes. That is a great way of doing it. Thanks so much for sharing this.
Hi, in that part of the instruction
The client_Id should be the Client ID found in Azure Portal.
The client_secret should be the Client Secret ID found in the Azure Portal.
The grant_type should be the text ‘client_credentials‘.
The Scope should be set to the URL of your D365 instance that you wish to connect to, followed by the text ‘/.default‘
There is probably an error: there is
The client_secret should by Client Secret ID found in the Azure Portal
Shouldn’t be ?
The client_secret should by Client Secret Value
Hi,
I am trying to use Postman in dev VM (https://usnconeboxax1aos.cloud.onebox.dynamics.com)
I am able to get token but when trying to get data from D365 receiving following error
{
“Message”: “Authentication failed.”,
“StackTrace”: null,
“ExceptionType”: “System.InvalidOperationException”
}
Any advise please?
You you need to get an access token first. Please follow the steps in this previous article first: https://dynamics365musings.com/setup-postman-to-call-d365-data-entities/
Thankyou for response.
Yes I followed this and getting token sucessfully. But when sending Get request to D365 entity getting this error.
Thankyou for response.
Yes I followed this and getting token sucessfully. But when sending Get request to D365 entity getting this error.
Have you setup a clientID and client secret in azure, and included those in your call? Also make sure you use the auth token in your get call. Also, the auth token expires in a few hours so make sure to make the call to get the auth token. Then use it in the get call fairly soon after that. If you wait awhile you need to get a new auth token.
Hi Peter,
Thank you for response.
Yes I have setup app in Azure and I am able to get token first and then sending request to D365 with token and I am sending get request just after getting token but getting error. Any idea?
It looks like it is an invalid operation exception. Are you trying to get a base Microsoft data entity? Or is it a custom one? If it is custom you should make sure it is marked as public, and that it is published, and you are using the correct name as listed in the properties of the data entity. I think to really help I would need to know the url, the body, and the headers (with access token not actually shown) to understand if all the required pieces are there.
In this article you explained about GET request and fetched data from the entity.
Can you please provide me any resource or link about the POST or PATCH request to update record in entity. I tried but not succeed.
Thank you very much for this great post, i tried this method but i get
{
“@odata.context”: “https://usnconeboxax1aos.cloud.onebox.dynamics.com/data/$metadata#CustomersV2”,
“value”: []
}
any idea?
Hi,
I am able to generate the access token successfully.
When i am trying to get the customer list and details it is showing the below message:
{
“@odata.context”: “https://ex-sdboxfef479a06b388a7fdevaos.axcloud.dynamics.com/data/$metadata#CustomersV2”,
“value”: []
}
Great article, learnt a lot. I am getting a “400 Bad Request” error message
Hi Peter,
Need your help in below error,
1. Token generated successfully
2. https:///data/CustomersV2 – In this step, I am getting below error..
{
“Message”: “Authentication failed.”,
“StackTrace”: null,
“ExceptionType”: “System.InvalidOperationException”
}
Thanks,
MG
It looks like you have three slashes after https: you should just have two.
You need http:///data/CustomersV2. Make sure this looks right and make sure the web call type is GET for this second call.
Probably too late but anyways
Make sure that the user you mapped on system administrator > setup > Azure active directory applications is pointing to any dataentity that have info
(settings > user options > preferences > Startup > Company)
Or define the entity to use on your get call