Getting an error when trying to authenticate using REST API Dynamics CRM with Azure AD

0 votes

I'm taking the following steps to access a Dynamics CRM Online REST API with Azure AD oAuth 2 Authentication:

- Registered a web app in Azure
- Configured the permissions to Dynamics CRM to have Delegated permissions "Access CRM Online as organization user"
- Created a Key with a 1 year expiration and kept the Client ID generated

After the web app was configured on Azure I created a Console app in .NET/C# that uses ADAL to make a simple request to retrieve a list of accounts: 
 

    class Program
{
    private static string ApiBaseUrl = "https://xxxxx.api.crm4.dynamics.com/";
    private static string ApiUrl = "https://xxxxx.api.crm4.dynamics.com/api/data/v8.1/";
    private static string ClientId = "2a5dcdaf-2036-4391-a3e5-9d0852ffe3f2";
    private static string AppKey = "symCaAYpYqhiMK2Gh+E1LUlfxbMy5X1sJ0/ugzM+ur0=";

    static void Main(string[] args)
    {
        AuthenticationParameters ap = AuthenticationParameters.CreateFromResourceUrlAsync(new Uri(ApiUrl)).Result;

        var clientCredential = new ClientCredential(ClientId, AppKey);

        var authenticationContext = new AuthenticationContext(ap.Authority);
        var authenticationResult = authenticationContext.AcquireToken(ApiBaseUrl, clientCredential);

        var httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authenticationResult.AccessToken);

        var result = httpClient.GetAsync(Path.Combine(ApiUrl, "accounts")).Result;         
    }
}

I can retrieve an access token successfully but when I try to do a httprequest to CRM I always get a 401 - Unauthorized status code. What am I missing?

Apr 25, 2019 in Azure by sabby
• 4,370 points
2,621 views

1 answer to this question.

0 votes

Dynamics 365 has started to support Server-to-Server (S2S) authentication. So, this code should work when you create an application user. For more information about how to make this authentication work check this website: https://msdn.microsoft.com/en-us/library/mt790170.aspx

Hope it helps!

To know more about Azure, join Azure course today.

Thank you!!

answered Apr 25, 2019 by Prerna
• 1,960 points

Related Questions In Azure

0 votes
2 answers

I am getting "Insufficient Privileges" error when trying to access Azure Graph APIs.

I had the same issue before, and ...READ MORE

answered Sep 17, 2019 in Azure by Ju Cheh
23,316 views
0 votes
1 answer
+1 vote
2 answers

How do I establish an connection to Azure IoT Hub? Say a connection like MQTT/AMQP using Python.

Azure IoT Hub comes with 3 SDK's: Service SDK Device ...READ MORE

answered Apr 13, 2018 in Azure by null_void
• 3,220 points
2,241 views
0 votes
1 answer

Which cloud hosting is best ( AWS or Azure or Google)

Hello @Hammer, This totally depends on your needs, ...READ MORE

answered Aug 10, 2018 in AWS by Priyaj
• 58,020 points
959 views
+15 votes
23 answers

AWS vs Azure which one will have a more promising career?

Azure is integrating with a lot of ...READ MORE

answered Jul 4, 2018 in Career Counselling by Soumodip Sinha
12,355 views
0 votes
1 answer

Azure has 3 different types of storage accounts. Is there any major difference between them?

Azure Storage provides three types of storage ...READ MORE

answered Mar 4, 2019 in Azure by Prerna
• 1,960 points
26,886 views
0 votes
1 answer

How to check if multiple files exist in Azure container?

foreach loop is the most efficient way though. ...READ MORE

answered Mar 4, 2019 in Azure by Prerna
• 1,960 points
3,878 views
0 votes
1 answer

How to add a body to a HttpWebRequest that is being used with the Azure Service management API?

The following code should help: byte[] buf = ...READ MORE

answered Apr 3, 2019 in Azure by Prerna
• 1,960 points
2,716 views
0 votes
1 answer

Azure: Getting error in loading 'Microsoft.TeamFoundation.WorkItemTracking.Client.Cache'

The error is occurring because you are trying ...READ MORE

answered Mar 26, 2019 in Azure by Prerna
• 1,960 points
993 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP