Reputation: 1
Using HTTP client in C# to interact with SharePoint. GET request run ok with json odata returned.
I am trying to get Form digest value using {siteurl}/_api/contextinfo and continually hitting a 401 response error. My code is below -
I have looked at https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/navigate-the-sharepoint-data-structure-represented-in-the-rest-service as well and other than a possible conflict with url formats shown on that page (i have tried both formats) - a. POST https://{site_url}/_api/contextinfo b. POST https://{site_url}/_api/doclib/contextinfo
the url string below builds to https://xxx.onmicrosoft.com/sites/sitename/_api/context
I am still getting the same error - can anyone suggest solution
` `url = @"https://" + HostSite + @"/" + SiteRoot + "/_api/contextinfo";
requestMessage = new HttpRequestMessage
{
Method = HttpMethod.Post,
Content = new StringContent("", Encoding.UTF8, "application/json"),
RequestUri = new Uri(url),
};
client.DefaultRequestHeaders.Add("Accept", "application/json;odata=verbose");
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken);
response = await client.SendAsync(requestMessage);
looked at SharePoint API guidance and it indicates that it should work but still get 401 error. Have set API permissions in Entra Id for Graph and SharePoint for Site manage all level.
Upvotes: 0
Views: 380
Reputation: 1
i guess, you are using ACS authentication. please don't use it. use Microsoft Entra ID. ACS Authentication is Deprecated
Upvotes: 0
Reputation: 1
After having this problem for days: the problem is, that to query the SharePoint API directly, a certificate is mandatory along with your client secret etc.
But you can alternatively switch to the Microsoft Graph REST API which works similar and has access to many SharePoint features - no certificate needed and you don´t need digest values at all.
The documentation is really lacking in many of these aspects.
Upvotes: 0