Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Not able to receive response from Dynamics API

(0) ShareShare
ReportReport
Posted on by 20

I have used MSAL.Net (followed blog https://msftblog.com/using-msal-net-with-dynamics-365-webapi-in-dotnet-core/) to communicate with Dynamics CRM which is returning the access token & when I give a call to products API I see below message & cannot move ahead.

Failed to call the Web API: Forbidden

Content: {"error":{"code": "0x80072560", "message": "The user is not a member of the organization."}}

However, if I login to dynamics 365 crm & copy the URL (https://{Company}.api.crm8.dynamics.com/api/data/v9.1/products) in the browser I am able to get the response.

Just a background:

Have Azure active subscription & Dynamics 365 30 day trial.

Any help on resolving the above error much appreciated.

Thanks & Regards,

Raghavendra Habbu

  • RE: Not able to receive response from Dynamics API

    Thank You Johao. Let me give it a try once again.

  • Suggested answer
    Johao Larios Profile Picture
    Johao Larios 1,795 on at
    RE: Not able to receive response from Dynamics API

    I have never used that to connect to d365 from .netCore.

    I use this:

     AuthenticationContext authContext = new AuthenticationContext("login.microsoftonline.com/" tenantId, false);

               ClientCredential credential = new ClientCredential(clientId, secret);

               AuthenticationResult result = await authContext.AcquireTokenAsync(serviceUrl, credential);

               if (result == null)

               {

                   throw new InvalidOperationException("Failed to obtain the JWT token");

               }

               string atoken = result.AccessToken;

               using (HttpClient client = new HttpClient())

               {

                   client.BaseAddress = new Uri(serviceUrl);

                   client.Timeout = new TimeSpan(0, 2, 0);  //2 minutes  

                   client.DefaultRequestHeaders.Add("OData-MaxVersion", "OData-MaxVersion");

                   client.DefaultRequestHeaders.Add("OData-Version", "4.0");

                   client.DefaultRequestHeaders.Accept.Add(

                       new MediaTypeWithQualityHeaderValue("application/json"));

                       HttpRequestMessage request =

                       new HttpRequestMessage(HttpMethod.Get, "/api/data/v9.0/opportunities?$select=name");

                   //Set the access token

                   request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", atoken);

                   HttpResponseMessage response = client.SendAsync(request).Result;

                    if (response.IsSuccessStatusCode)

                   {

                       //Get the response content and parse it.  

                       JObject body = JObject.Parse(response.Content.ReadAsStringAsync().Result);

                       //  Guid userId = (Guid)body["UserId"];

                       //  Console.WriteLine("Your system user ID is: {0}", userId);

                       Console.WriteLine(body.ToString());

                       Console.ReadLine();

                   }

               }

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,735 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,466 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans