web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to Transact data from my API to Dynamic 365 API

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Suggested answer
    Michel van den Brink Profile Picture
    4,697 on at

    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:

    https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/connect-customer-engagement-web-services-using-oauth

      

    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:

    https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/xrm-tooling/use-crmserviceclient-constructors-connect

    Quick start sample for CrmServiceClient:

    https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/xrm-tooling/sample-quick-start-xrm-tooling-api 

    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
    });

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
AS-17030037-0 Profile Picture

AS-17030037-0 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans