I always get an 401 error response when making a request to get the top 3 accounts out of the crm.
The authentication looks like it worked i got a accesstoken as response.
What am I doing wrong?
public async void Authenticate()
{
AuthenticationContext authContext = new AuthenticationContext(_authority, false);
ClientCredential credentials = new ClientCredential(_clientId, _appKey);
_authResult = authContext.AcquireToken("https://graph.windows.net", credentials);
var token = _authResult.AccessToken;
using (HttpClient httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri(_serviceUrl);
httpClient.Timeout = new TimeSpan(0, 2, 0);
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", _authResult.AccessToken);
HttpResponseMessage accountResponse = await httpClient.GetAsync("/api/data/v8.0/accounts?$select=name&$top=3 HTTP/1.1");
if (accountResponse.IsSuccessStatusCode)
{
JObject jAccountResponse =
JObject.Parse(accountResponse.Content.ReadAsStringAsync().Result);
_logger.Information("API TEST");
}
else
return;
}
}









