DEV Community

Benjsoft
Benjsoft

Posted on

How to obtain an OAuth access token in Dynamics CRM 365 Online using C#

The following code snippet demonstrates how to obtain an OAuth access token from your Dynamics CRM online instance that you can use to pass as authenticated and authorization when accessing Dynamics CRM 365 web services in your client-side or server-side code.

using (HttpClient client = new HttpClient())
{
   client.BaseAddress = new 
       Uri("https://login.microsoftonline.com/common/oauth2/token");

   HttpRequestMessage request = new HttpRequestMessage();
   request.Method = HttpMethod.Post;

   var keysValues = new List<KeyValuePair<string, string>>();
   keysValues.Add(new KeyValuePair<string, string>("client_id", 
       "2ad88395-b77d-4561-9441-d0e40824f9bc"));
   keysValues.Add(new KeyValuePair<string, string>("resource", url));
   keysValues.Add(new KeyValuePair<string, string>("username", username));
   keysValues.Add(new KeyValuePair<string, string>("password", password));
   keysValues.Add(new KeyValuePair<string, string>("grant_type", "password"));

   request.Content = new FormUrlEncodedContent(keysValues);

   HttpResponseMessage response = client.SendAsync(request).Result;
   return response.StatusCode == HttpStatusCode.OK;
}

In the code snippet above, the only thing that will change are username and password, there are your Dynamics 365 credentials to authenticate and authorize your instance, also the URL for your Dynamics CRM instance (e.g. https://benjsoftdemo.crm3.dynamics.com).

The client_id is the default Client Id which is setup against Dynamics 365 Online instances.

You can test the following code snippet using Postman.

When successful, the response will contain the JSON response payload that contains your access_token.

Then you can use the access_token programmatically to authenticate and authorize you when accessing the CRM webservice calls.

For more information, you can refer to the following link Dynamics 365 Online Authenticate with User Credentials.

Top comments (6)

Collapse
 
hpandalai profile image
hpandalai

Hi,
I have tried the above approach to connect to our Dynamics 365 online. Where would I get the client_id from? I tried the value you provided thinking its a constant but I get an error back saying AADSTS50126: Invalid username or password. But I can login with the account thru the ui. What would be the problem? I tried our dev test environment and getting the same message. Many Thanks.

Regards,
Hari

Collapse
 
ashv profile image
Ashish Vishwakarma • Edited

Find end to end tutorial here, clientId comes from Azure AD App, which you need to create.

ashishvishwakarma.com/Dynamics-365...

Collapse
 
benjsoft profile image
Benjsoft

The client id that is generated for your app when you register it in Azure

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
benjsoft profile image
Benjsoft

Here is an alternative approach

youtube.com/watch?v=Td7Bk3IXJ9s

Collapse
 
vaibhavrmore1 profile image
Vaibhav More

It gives below error:
error_description: "AADSTS50059: No tenant-identifying information found in either the request or implied by any provided credentials.
Trace ID: 2cf67843-cb21-4793-b547-5fdd119d6600
Correlation ID: f0d142b3-5dd5-42c4-9915-6f397c89f3e3
Timestamp: 2020-08-17 14:02:27Z"