Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics RMS (Archived)

Microsoft Dynamics 365 for Retail API

(0) ShareShare
ReportReport
Posted on by

Hi, I'm developing an application which migrate data from some old database/system into Mycrosoft Dynamics 365 For Retail. 

As I'm following some tutorials, I've come up to these code:

        private HttpClient CreateRequest()
        {
            AuthenticationContext authContext = new AuthenticationContext(_authority, false);
            ClientCredential credentials = new ClientCredential(_clientID, _clientSecret);
            //_resource = "d365rtrialha-10.retail.dynamics.com";
            var task = authContext.AcquireTokenAsync(_resource, credentials);
            task.Wait();
            _authResult = task.Result;
            var token = _authResult.AccessToken;
            var client = new HttpClient();
            client.BaseAddress = new Uri(_resource);
            client.Timeout = new TimeSpan(0, 2, 0);
            client.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
            client.DefaultRequestHeaders.Add("OData-Version", "4.0");
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization =
                new AuthenticationHeaderValue("Bearer", _authResult.AccessToken); // SUCCESS

            return client;
        }

        public async Task WhoAmI()
        {
            using (var client = CreateRequest())
            {
                HttpResponseMessage whoAmIResponse = await client.GetAsync("api/data/v8.2/WhoAmI");
                var content = await whoAmIResponse.Content.ReadAsStringAsync();
                if (!whoAmIResponse.IsSuccessStatusCode)
                {

                    /**

                    * This function always raised 500: Internal Server Error

                    **/
                    throw new HttpRequestException(whoAmIResponse.ReasonPhrase);
                }
            }
        }

From within the CreateRequest function, I got an AccessToken successfully without any error. However, when calling API to get logged user ("WhoAmI"), I've got 500: Internal Server Error. Am I doing something wrong?

*This post is locked for comments

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 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,261 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 233,013 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans