Azure Tenant ID
Dynamics 365 CRM URL
Application ID
(Azure AD App Registration) andApplication Secret
API Permission
for Dynamics 365 withuser_impersonation
scopeApplication User
created in Dynamics 365 using Application ID andinterattion mode
set toNon-interactive
- Use
Basic
authentication (Base64 ofApplicationId:ApplicationSecret
concatenation) to obtain the OAuth2 Access Token from the Microsoft Identity Platform (https://login.microsoftonline.com/{YOUR Azure Tenant ID}/oauth2/v2.0/token
) accept
Header value must be set toapplication/x-www-form-urlencoded
grant_type
Header value must be set toclient_credentials
scope
Header value is usually set to.default
- Send the POST request
- Read the JSON response
- Deserialize JSON into
OAuth2Token
variable
The OAuth2 Access Token will contain the token type (usually Bearer
) and the token expiration time.
The OAuth2TokenProvider
caches the token so that we don't re-auth too frequently.
Cached Tokens will expire automatically when they reach the end of ther lifetime (DateTime.Now + token.ExpiresIn
).
OAuth2 Access Tokens can now be used for authenticating against Dynamics 365 CRM to perform OData queries.
- Valid Access Token
- Obtain OAuth2 Token as shown above
- Set up HTTP Client
Authorization
Header value to use OAuth2 token type (usuallyBearer
) and concatenate theAccessToken
value (e.g.Bearer <some crazy looking string>
)- Formulate the CRM Uri (e.g.
https://mycompany.crm.dynamics.com/api/data/v9.1
) and OData query Uri - Attach the OData query Uri as querystring parameters
- Send the GET request
- Read JSON Response
- Deserialize JSON into Models
DONE.