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

2 Answers 2

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());
3
  • May i know how do i get the AccessToken for calling sharepoint Rest Api in the java? please help me in this Commented May 9, 2016 at 5:32
  • Please look into it...stackoverflow.com/questions/11804624/… Commented May 9, 2016 at 7:28
  • I think the above code is not java , but question is asking something related to java. also the link given in the comment is javaScript everything looks unrelated. Commented Oct 22, 2024 at 10:53
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 and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.