Connect to Dynamics 365 Common Data Service with OAuth 2.0

Recently, I had a requirement to connect to CDS using OAuth 2.0 so that an AAD token can be generated without having to prompt a user for sign in. I have listed out the steps below to achieve this.

App Registration

  1. Login to azure portal.
  2. Navigate to 'Azure Active Directory' > 'App registrations' > 'New registration'


  3. Enter the name of the app register and click 'Register' to create a new app registration


  4. Open 'API permissions' within the app registration, click 'Add a permissions' and choose 'Dynamics CRM'



  5. Select 'Delegated permissions', check 'user_impersonation' and click 'Add permission'


  6. Click 'Grant admin consent'


  7. Navigate to 'Certificates & secrets' and generate a 'New client secret'. Keep a copy of the client secret. 
  8. Navigate to overview and copy the 'Client Id/Application Id'
  9. Click Endpoints and copy the 'OAuth 2.0 token endpoint'

Common Data Service

  1. Login to your CDS environment and navigate to 'Advanced Settings' > 'Security' > 'Users'


  2. Select 'Application Users' and click 'New'

  3. Enter the 'Name', 'User Name' and 'Email Address' for the user. Enter the 'Application ID' that was copied earlier. When the record is saved the 'Application ID URI' and 'Azure Object ID' will be automatically created.


  4. Click on manage roles and assign 'System Administrator' role to the user.

Generate AAD Token

The AAD token can be generated as follows;

clientId -  this is the application Id of the app registration created earlier
clientSecret - this is the secret generated for the app registration earlier
url - this is the OAuth 2.0 token endpoint
resourceURI - this is the API url, for example, https://orgxxx.crm.dynamics.com

ClientCredential clientCrendential = new ClientCredential(clientId, clientSecret);
AuthenticationContext authContext = new AuthenticationContext(url);
AuthenticationResult authResult = await authContext.AcquireTokenAsync(resourceUri, clientCrendential);
string token = authResult.AccessToken;

Read data

I was able to extract data from entities using the following code;

URL - this is the odata url for example, https://orgxxx.crm.dynamics.com/api/data/v9.0/

HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

httpClient.BaseAddress = new Uri(URL);
HttpRequestMessage getRequest = new HttpRequestMessage(HttpMethod.Get, queryOptions);

HttpResponseMessage result = await httpClient.SendAsync(getRequest);

Comments

Popular posts from this blog

D365FO - Create a multi-select lookup in batch jobs

Generate and download a csv file using X++

Creating a batch job with query based filter in D365 back-office