My requirement is to Sync my database record to Dynamic 365 and also to authenticate the user.
I am trying with this code but it is not helping me out, Please help me to connect and resolve it
string api = "ujwl.api.crm8.dynamics.com/.../v9.0";
AuthenticationParameters ap = AuthenticationParameters.CreateFromResourceUrlAsync(new Uri(api)).Result;
var creds = new ClientCredential("xxx", "xxx");
AuthenticationContext authContext = new AuthenticationContext(ap.Authority);
var token = authContext.AcquireTokenAsync(ap.Resource, creds).Result.AccessToken;
using (HttpClient httpClient = new HttpClient())
{
httpClient.Timeout = new TimeSpan(0, 2, 0);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
HttpResponseMessage response = await httpClient.GetAsync("ujwl.api.crm8.dynamics.com/.../accounts$top=2");
}
I am not able to authenticate user nor getting the data from the server.
Is this approach correct or I need to go with the different approach.
*This post is locked for comments
Hello,
The code snippet you are using will unfortunately not work, as is, it is not meant to authenticate with with an application id and secret, not a user's username and password.
You would need to create an application registration in Azure authenticate with OAuth 2.0
This article explains how that works:
May I suggest though that, for simplicy, you take a look at the CrmServiceClient class from the XrmTooling library provided my Microsoft. It makes connecting to Dynamics 365 (CRM) Online much easier and is a great wrapper around the Web API, taking care of most of the heavy lifting and allowing you to focus on your code, rather than creating API calls
How to connect with CrmServiceClient:
Quick start sample for CrmServiceClient:
Your code sample, translated to using the CrmServiceClient would look like this:
var client = new Microsoft.Xrm.Tooling.Connector.CrmServiceClient("username@companyname", CrmServiceClient.MakeSecureString("password"), "crm8", // region "ujwl", // organizationname useUniqueInstance: true, useSsl: true, isOffice365: true); Microsoft.Xrm.Sdk.EntityCollection accounts = client.RetrieveMultiple(new Microsoft.Xrm.Sdk.Query.QueryExpression { EntityName = "account", TopCount = 2 });
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,253 Super User 2024 Season 2
Martin Dráb 230,188 Most Valuable Professional
nmaenpaa 101,156