Sunday, April 2, 2017

Implementing SharePoint Online REST client on JAVA platform.

Hi folks,

SharePoint 2013 hosts a Representational State Transfer (REST) service. Using REST API, Developers can interact remotely with SharePoint data by using any technology that supports REST web requests.

SharePoint Online (SPO) REST client request should need to authorize. SharePoint Online Single Sign On (SSO) process validate the REST call and establish connection between SPO and REST client.

In order to establish connection between SPO and REST client, needs to provide following authentication values.

1) rtFa Cookies
2) FedAuth Cookies
3) FormDigestValue

In this post, I’ll demonstrate only implementing a JAVA based RESTful client part and way of passing the above mention authentication values as headers in RESTful call. How to generate the auth values explained in this post. We can simply use generated auth values using POSTMAN case, these values never expire and platform independent.

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sharepoint.online.rest.demo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
/**
*
* @author Denuwan Himanga Hettiarachchi
*/
public class SharePointOnlineRESTDemo {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Set values, which are generated using POSTMAN RESTful call
String rtFa = "";
String fedAuth = "";
String XRequestDigest = "";
//Set file path
String filePath = "";
SharePointOnlineRESTDemo call = new SharePointOnlineRESTDemo();
call.getFolderByServerRelativeUrl(rtFa, fedAuth, XRequestDigest, filePath);
}
private void getFolderByServerRelativeUrl(String rtFa, String fedAuth, String XRequestDigest, String filePath) {
DefaultHttpClient httpClient = new DefaultHttpClient();
try {
HttpGet getRequest = new HttpGet(
"https://cielotool.sharepoint.com/sites/Documents/_api/web/GetFolderByServerRelativeUrl('"+filePath+"')/Files");
getRequest.addHeader("Cookie", "rtFa="+rtFa+";FedAuth="+fedAuth);
getRequest.addHeader("Authorization", "Bearer"+XRequestDigest);
//Optional header which format response as a JSON object
getRequest.addHeader("accept", "application/json");
HttpResponse response = httpClient.execute(getRequest);
if (response.getStatusLine().getStatusCode() == 200) {
BufferedReader br = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));
String output;
while ((output = br.readLine()) != null) {
System.out.println(output);
}
}else{
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatusLine().getStatusCode());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
httpClient.getConnectionManager().shutdown();
}
}
}


Happy Coding....

Regards,
Denuwan Himanga