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 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

I have the same question (0)

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 RMS (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans