I'm trying to access the rest API of Excel services in SharePoint online from a webjob in C#.
I have a client secret and ClientId of an registered add-in and can make Rest request to normal lists and items on the site, but when I tries to access Excel services I get an 401. I can make requests in the Web client to Excel services without any problems.
I use the following code (more or less) and if I change the url to url = tmpURL + "/_api/web/lists/getbyTitle('Projekt')/Items" it works as a charm (so the accessToken should be fine).
String tmpURL = "https://XXXX.sharepoint.com/sites/SiteXXX";
string accessToken = GetAppOnlyAccessToken(tmpURL);
String url = tmpURL + "/_vti_bin/ExcelRest.aspx/fileFolder/fileName.xlsx/Model/Ranges('Sheet1!C8%7CG21')";
HttpWebRequest endpointRequest =
(HttpWebRequest)HttpWebRequest.Create(url
);
endpointRequest.ContentType = "application/json;odata=verbose";
endpointRequest.Accept = "application/json;odata=verbose";
endpointRequest.Headers.Set("Authorization", "Bearer "+ accessToken);
endpointRequest.Headers.Add("IF-MATCH", "*");
using (HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse())
{
using (StreamReader reader = new StreamReader(endpointResponse.GetResponseStream()))
{
string result = reader.ReadToEnd();
Console.WriteLine(result);
}
}
What do I do wrong?