1

I'm trying to list items from some list in Office 365 SharePoint from a java native Windows app.

I'm using deprecated office-365-java-sdk to authenticate and get an access token. Yes, this SDK is deprecated but authentication still works. So, I have an access token.

So, next step is to make GET request. In Graph Explorer this URL works fine:

/v1.0/sites/root/lists/{site-id}/items

I followed documentation to build the request and I have to add a header with authentication token so this is my code:

StringBuilder result = new StringBuilder();
URL url = new URL("https://graph.microsoft.com/v1.0/sites/root/lists/{0a506dcb-ecbc-40ed-bf2c-5912e78e3ca8}/items");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Authorization", "Bearer " + access_token);
conn.setRequestProperty("Content-type", "application/json");
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null)
{
    result.append(line);
}
rd.close();
System.out.println(result.toString());

Authentication is working, because if access token header is not added, it returns a status error code of 401 Required authentication information is either missing or not valid for the resource. But with an access code, it returns error code 400 Cannot process the request because it is malformed or incorrect.

I'm stuck with this, I read the documentation again and again and after checking URL is right using Graph Explorer, I don't know if this is not the right way to include headers or what....

2
  • change the line conn.setRequestProperty("Content-type","application/json"); to conn.setRequestProperty("accept","application/json;odata=verbose"); and check Commented Oct 30, 2017 at 11:48
  • You're right! key of header should be "accept". however i had to delete "odata=verbose" because was returning bad request also. I put the request on Postman and "application/json;odata=ver‌​bose" is not supported. Please put the comment as respone and ill upvote you. Commented Oct 30, 2017 at 13:25

1 Answer 1

1

The correct header to pass is Accept: application/json.

So, replace the conn.setRequestProperty("Content-type","application/json"); with

conn.setRequestProperty("Accept","application/json");

Sign up to request clarification or add additional context in comments.

Comments

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.