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.

  • meetdynamics Profile Picture
    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
    Filipe Relvas Profile Picture
    on at
    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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

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

#1
Siv Sagar Profile Picture

Siv Sagar 149 Super User 2025 Season 1

#2
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 53 Super User 2025 Season 1

#3
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 32 Super User 2025 Season 1

Overall leaderboard

Product updates

Dynamics 365 release plans