What is Issue?
MS CRM provides out-of-box Web API which we mostly use in javascript to get data from entities whenever required. But there are many situations where your company uses different applications and CRM is one of them. The data across company can be used in any of these application in real time. So instead of writing custom Web API, if MS CRM provides out-of-box Web API, can we use that in any other application? or is it possible to call MS CRM APIs using postman for testing purpose before used in any other application?
The Reason-
MS CRM provides the way to get data from entities with calling Web API. But mostly we use this API calls within CRM application. When calling Web API within CRM , user is already logged in to the CRM which do not need any extra authorization. When we try to calls these Web API out side CRM it requires authorization to access data from CRM.
Solution-
To Call Web APIs outside MS CRM environment you need to get Bearer token for the user using OAuth2.0 authorization.
Lets take example – WebAPI to get all Accounts- https://xxxxx.crm8.dynamics.com/api/data/v8.2/accounts
Lets generate bearer token to access above WebAPI from Postman-
I am considering You have registered the Azure app and you have Client Id and Client secrete with you for this app.
In Postman- New request add your request –
-
- Go to the Authorization tab
- Select Type – OAuth2.0
- Click on Get New Access Token
- In popup window please enter all required information as below-
- Grant Type – Authorization Code
- Enter Callback URL provided in Azure App – Redirect URI
- Enter Auth URL –
https://login.windows.net/common/oauth2/authorize?resource=https://xxxxx.crm8.dynamics.com
- Access Token URL –
https://login.microsoftonline.com/common/oauth2/token
- Enter Client Id and Client Secret you have.(Azure App client details).
- Click on Request Token.
- Microsoft login window will popup. Enter your credentials to login.On successful authentication, It will bring an access token for you.
- Select Access Token you have generated just a while before from Available tokens in postman.
- Don’t forget to select Add authorization data to – Request Header
- Now you can send the request and test your MS CRM Web APIs.
- You can use the Code option in postman to generate code according to your programming needs.
You can use the same parameters when generating bearer token for the user using code.
How to get bearer token using Postman-
- Use Post Method and URL-
https://login.microsoftonline.com/common/oauth2/token
- Add below details to Body of the request as form-data-
– grant_type – client_credentials
– user’s credentials : username and password
– other details – client_id and client_secret as described above. -resource : your MS CRM url.
Using the bearer token generated you can access MS CRM Web APIs in any application.
Hope this will help…
Enjoy Ms CRM!!!
Follow on Facebook- FB: MSCRM16Tech