Skip to Content

Overview:

SharePoint supports OAuth to authorize the user instead of credentials (username and password) while calling SharePoint-REST to perform CRUD operations.

Open Authorization (OAuth) is an open protocol for authorization.

OAuth enables secure authorization from desktop and web applications in a simple and standard way. It lets users approve an application to act on their behalf without sharing their user name and password.

To perform any Read/Write operation using respective SharePoint-REST service, first we require “SharePoint Access token” for authentication.

To fetch “SharePoint Access token” following REST-details can be used:

  • REST-URL:   https://accounts.accesscontrol.windows.net/<tenantID>/tokens/OAuth/2
  • Method:         POST
  • Headers:
    • Authorization: “Bearer ” + accessToken
      Content-Type: “application/x-www-form-urlencoded”
  • Body: (Request Parameter format)
    • grant_type=client_credentials
      &client_id=<shp_clientId>@<shp_tenantId>
      &client_secret=<shp_clientSecret>
      &resource=00000003-0000-0ff1-ce00-000000000000/<org_Shp_Host>@<shp_tenantId>
  • Note:
    • Following details are specific to Organization’s SharePoint data:
      • <shp_clientId>:          Client ID is a GUID for the SharePoint Add in
      • <shp_tenantId>:         Tenant ID is SharePoint Online Site Realm
      • <shp_clientSecret>:   Client Secret is the password for the add-ins.
      • <org_Shp_Host>:       Organisation’s SharePoint host name

SAP PI UDF example to fetch SharePoint Access Token:

Java Imports required in UDF are as :
  • java.net.URL
    java.net.URLConnection
    java.net.HttpURLConnection
Java Input parameters:
Java UDF Code:
public String getShpToken(String shp_clientId, String shp_tenantId, String shp_clientSecret, String shp_clientDomain, Container container) throws StreamTransformationException{
		 /**
		 * This function helps to get SharePoint Access Token. SharePoint Access
		 * Token is required to authenticate SharePoint REST service while performing Read/Write events. 
		 * SharePoint REST-URL to get access token is as:
		 * 	https://accounts.accesscontrol.windows.net/<tenantID>/tokens/OAuth/2
		 * 
		 * Input required related to SharePoint are as: 
		 * 1. shp_clientId 
		 * 2. shp_tenantId
		 * 3. shp_clientSecret 		
		 */

		String accessToken = "";
		try {

			// AccessToken url
			String wsURL = "https://accounts.accesscontrol.windows.net/" + shp_tenantId + "/tokens/OAuth/2";

			URL url = new URL(wsURL);
			URLConnection connection = url.openConnection();
			HttpURLConnection httpConn = (HttpURLConnection) connection;

			// Set header
			httpConn.setRequestProperty("Content-Type",	"application/x-www-form-urlencoded");
			httpConn.setDoOutput(true);
			httpConn.setDoInput(true);
			httpConn.setRequestMethod("POST");

			// Prepare RequestData
			String jsonParam = "grant_type=client_credentials" 
					+ "&client_id=" + shp_clientId + "@" + shp_tenantId 
					+ "&client_secret=" + shp_clientSecret
					+ "&resource=00000003-0000-0ff1-ce00-000000000000/<org_Shp_Host>@" + shp_tenantId;
			//Here, <org_Shp_Host> is "Origanisations's Sharepoint Host"

			// Send Request
			DataOutputStream wr = new DataOutputStream(httpConn.getOutputStream());
			wr.writeBytes(jsonParam);
			wr.flush();
			wr.close();

			// Read the response.
			InputStreamReader isr = null;
			if (httpConn.getResponseCode() == 200) {
				isr = new InputStreamReader(httpConn.getInputStream());
			} else {
				isr = new InputStreamReader(httpConn.getErrorStream());
			}

			BufferedReader in = new BufferedReader(isr);
			String responseString = "";
			String outputString = "";

			// Write response to a String.
			while ((responseString = in.readLine()) != null) {
				outputString = outputString + responseString;
			}

			// Extracting accessToken from string, here response (outputString)is a Json format string
			if (outputString.indexOf("access_token\":\"") > -1) {
				int i1 = outputString.indexOf("access_token\":\"");
				String str1 = outputString.substring(i1 + 15);
				int i2 = str1.indexOf("\"}");
				String str2 = str1.substring(0, i2);
				accessToken = str2;
			}
		} catch (Exception e) {
			accessToken = "Error: " + e.getMessage();			
		}
		return accessToken;
}
Testing Screen of UDF inside Graphical Map:

 

 

<<<<< Parent blog reference…..  Integrate SharePoint using SAP PI

To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

Leave a Reply