0

I am struggling to retrieve an entity records in Microsoft ERP Dynamics Finance and Operations. I was able to get authentication access but when I use the http request it show me an error.

My code:

 string clientId = "clientId";
        string clientSecret = "clientSecret";
        string authority = "https://login.microsoftonline.com/tenantId";
        string resourceUrl = "https://trial-ub2dbh.sandbox.operations.dynamics.com/"; // Org URL

        try
        {
            ClientCredential credentials = new ClientCredential(clientId, clientSecret);
            var authContext = new Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext(authority);
            var result = await authContext.AcquireTokenAsync(resourceUrl, credentials);

            HttpClient httpClient = null;
            httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
            httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
            httpClient.BaseAddress = new Uri("https://trial-ub2dbh.sandbox.operations.dynamics.com/data/$metadata#");

            var response_Contacts = httpClient.GetAsync("PartyContacts").Result;

        }
        catch(Exception ex)
        {
            return ex.ToString();
        }

The error is :

bad request

How can I make this retrieve?

FH-Inway
  • 4,432
  • 1
  • 20
  • 37

1 Answers1

0

The url should be different, for retrieving data - the endpoint should be different from $metadata query.

You have to use below endpoint for retrieving Vendors.

httpClient.BaseAddress = new Uri("https://trial-ub2dbh.sandbox.operations.dynamics.com/data/");
var response = httpClient.GetAsync("Vendors").Result;

instead of below line:

httpClient.BaseAddress = new Uri("https://trial-ub2dbh.sandbox.operations.dynamics.com/data/$metadata#");