Skip to main content

Notifications

Announcements

No record found.

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

Error in HttpResponseMessage in GetAsync() while trying to retrieve accounts data after receiving access token HttpClient

(0) ShareShare
ReportReport
Posted on by 358

Hi everyone,

I am trying to retrieve accounts data through WeAPI via Dynamics 365 On Premise which has IFD authetication. I am able to get the access token through adfsurl/.../token but when I pass the token and try to get some data using WebAPI, I am facing 401 unauthorized error. Here is my code : 

var res = _httpClient.PostAsync(new Uri("">adfsurl/.../token"), content);
var respo = res.Result.Content.ReadAsStringAsync().Result;
accesstoken = JObject.Parse(respo).GetValue("access_token").ToString();
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer",accesstoken);

var response = _httpClient.GetAsync("">crmurl/.../WhoAmI").Result;

I am getting this error on the above line of code (StatusCode: 401, ReasonPhrase: 'Unauthorized')

Any help is highly appreciated. Thank you.

  • meetdynamics Profile Picture
    meetdynamics 358 on at
    RE: Error in HttpResponseMessage in GetAsync() while trying to retrieve accounts data after receiving access token HttpClient

    Hi Filipe,

    Thanks for the response. We did registered the application on ADFS and tried to access D365 Web API, but we still got errors. Finally we switched to intergating using web services.

    Anyways, thanks for helping me out.

  • Suggested answer
    RE: Error in HttpResponseMessage in GetAsync() while trying to retrieve accounts data after receiving access token HttpClient

    Hi Mithali,

    For that scenario you might also need to register an application on ADFS. While the token is valid for direct access through the client an external app that requires access to the OrganizationService or the RESTful endpoint such as the WebAPI might require it.

    I suggest to take a look at this:

    docs.microsoft.com/.../walkthrough-register-app-active-directory

    WebAPI sample that you might want to use:

    docs.microsoft.com/.../mt779074(v=crm.8)

    Hope this helps!

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans