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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
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.

I have the same question (0)
  • Suggested answer
    Filipe Relvas Profile Picture
    on at

    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!

  • meetdynamics Profile Picture
    358 on at

    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.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 74

#3
Martin Dráb Profile Picture

Martin Dráb 64 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans