Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Talking to Dynamics CRM Online thru Web API

(0) ShareShare
ReportReport
Posted on by 225

I'm trying to create a new contact (proof of concept) thru web api, but om getting 401 Unauthorized. I'll post my code below:

try
{
    var jsonStr = "{ \"firstname\": \"Test\", \"lastname\":\"API\" }";

    string username = "lars@foo.onmicrosoft.com";
    string password = "qwert1234";
    string domain = "foo.onmicrosoft.com";
    string baseAddress = "https://foo.crm4.dynamics.com";

    byte[] credentialBuffer = new UTF8Encoding().GetBytes(username + ":" + password);

    var request = (HttpWebRequest)WebRequest.Create(baseAddress + "/api/data/v9.1/contacts");
    request.Method = "POST";
request.ContentType = "application/json"; request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(credentialBuffer));  request.Headers.Add("OData-MaxVersion", "4.0"); request.Headers.Add("OData-Version", "4.0"); using (var stream = request.GetRequestStream()) { var jsonStrBytes = Encoding.ASCII.GetBytes(jsonStr); stream.Write(jsonStrBytes, 0, jsonStrBytes.Length); } var response = (HttpWebResponse)request.GetResponse(); var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); } catch (Exception ex) { }

But from this I get 401 Unauthorized. This is Dynamics CRM Online and my user is a 365 user. What am I missing here?

*This post is locked for comments

  • jlattimer Profile Picture
    jlattimer 24,558 on at
    RE: Talking to Dynamics CRM Online thru Web API

    If you do't use the SDK you'll need to craft all the HTTP requests - something you'd probably do if using .NET wasn't an option. The example in the Tip of the Day article shows using OAuth with the SDK

  • _Lars_ Profile Picture
    _Lars_ 225 on at
    RE: Talking to Dynamics CRM Online thru Web API

    Hello Jason, thank you for your answer!

    Whats the difference from using "raw" https like mye code above and the SDK (Microsoft.Xrm.Sdk.IOrganizationService)? The CrmServiceClient in Microsoft.Crm.ToolingConnector only takes a simple connection string, and no Azure Ad or Client ID/Secret are required?

  • Verified answer
    jlattimer Profile Picture
    jlattimer 24,558 on at
    RE: Talking to Dynamics CRM Online thru Web API

    You need to register an application in Azure AD and use the corresponding Client ID/Secret for the application user created:

    https://crmtipoftheday.com/767/server-to-server-authentication-is-here/

    Then the code would look something like this:

    https://github.com/jlattimer/CrmWebApiCSharp/blob/master/CrmWebApiCSharpNoAdal/Program.cs

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,489 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans