Mihir

Mihir

  • NA
  • 91
  • 2.7k

How to call REST api in provider hosted app

Jun 8 2018 9:38 AM
I want to call the Site Design apis in my provider hosted app to install site designs. Site design apis are REST apis. But when I trying to call it is always giving me the 403 forbidden error. I am passing the request digest value as well. To test the REST query in provider hosted app I am just calling the lists from a site collection. But it is giving me 403 error. Below is my code, What I am doing wrong here?
  1. string urlToRequest = clientContext.Web.Url + "/_api/web/lists";  
  2. HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create(urlToRequest);  
  3. endpointRequest.Method = "GET";  
  4. FormDigestInfo formDigest = clientContext.GetFormDigestDirect();  
  5. endpointRequest.UseDefaultCredentials = true;  
  6. endpointRequest.Accept = "application/json; odata=verbose";  
  7. endpointRequest.ContentType = "application/json;odata=verbose";  
  8. endpointRequest.Headers.Add("X-RequestDigest", formDigest.DigestValue);  
  9. HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse();

Answers (3)