3

I want to use the REST API for getting the list as mentioned in Complete basic operations using SharePoint 2013 REST endpoints

When I am trying to call https://siteurl/_api/web/lists in the rest client. I get the following error

<?xml version="1.0" encoding="utf-8"?><m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><m:code>-2147024891, System.UnauthorizedAccessException</m:code><m:message xml:lang="en-US">Access denied. You do not have permission to perform this action or access this resource.</m:message></m:error>

1

Unfortunately, the REST services are only working for authenticated users. It doesn't work with anonymous access in 2013.

SharePoint 2013 APIs for anonymous and/or mobile access

But You need to set the Bearer property in the request header with Authorization token. Please look into below mentioned code.

SharePointContextToken ContextToken = TokenHelper.ReadAndValidateContextToken(ContextTokenString, Request.Url.Authority);

Uri sharepointUrl = new Uri(Request.QueryString["SPHostUrl"]);

//Get the AccessToken
String AccessToken = TokenHelper.GetAccessToken(ContextToken,sharepointUrl.Authority).AccessToken;

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(@"https://yoursite.sharepoint.com/_api/web/lists");
request.Method = "GET";
request.Accept = "application/json;odata=verbose";
request.Headers.Add("Authorization", "Bearer " + AccessToken);

HttpWebResponse response =(HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
0

All request to the SharePoint REST API are made using the context of the current user. Since you are trying to call SharePoint REST API in your external java application, it is not able to authenticate the user thereby returning 403 Forbidden error.
Please go through the below link on how to configure Basic authentication from Java to SharePoint
BASIC authentication from JAVA to Sharepoint 2013 REST API

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.