Note: Please do not make my question as duplicate, still i haven't get any proper answer for my question.
I am getting 401 unauthorized error when calling a sharepoint search rest api (/_api/search/query?querytext) from a .net application using claims based authentication.
My request and error as below
httpWebRequest.Headers["X-RequestDigest"] = GetDigestForPostRequest;
httpWebRequest.Headers["Authorization"] = "Bearer " + generateClaimsIdentityToken;
GetSearchResponse(httpWebRequest)
private void GetSearchResponse(HttpWebRequest httpWebRequest)
{
try
{
using (var httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse)
{
using (var stream = httpWebResponse.GetResponseStream())
{
ProcessSearchResponse(stream);
}
}
}
catch (WebException ex)
{//catch and log the error
}
stack trace : "{\"error\":{\"code\":\"-2147024891,
System.UnauthorizedAccessException\",\"message\":{\"lang\":\"en-US\",\"value\":\"Access
denied. You do not have permission to perform this action or access
this resource.\"}}}"
[MessageSecurityException: The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was ''.]