Integrating SharePoint Data Into Node.JS With Access Token


In this article, you will learn how to integrate SharePoint applications data into node applications with app authentication approach.


The scenario is similar to the previous part, but only the authentication technique changes. In my previous article, I have explained about integrating the SharePoint data into Node.JS applications by hard-coding the user credentials though that is not the right approach.

For authentication, same node packages will be used. Let us deeply look into the step by step approach for accomplishing the tasks.

How it works

Here, let us look at authenticating the SharePoint data from Node.JS applications using app context. The client app is registered on the portal with client ID and secret. The client credentials is then passed to get the access token for authorization in the node application. Once the authentication token is received, token is set in the header for GET/POST REST API calls of node application to access the SharePoint data. 
Steps Involved

Register the app on the SharePoint portal. App can be registered from AppRegNew.aspx page. Navigate to the registration page, and fill in the required details. The below snapshot shows the same.

Generate the client ID and client secret, and fill in the required details and click on "Create" to create the app. Once done, copy and keep the ID and secret values for later use.

To lookat the created  app and provide the necessary permissions, navigate to http://siteurl/_layouts/15/AppInv.aspx page. Paste the client id and look up the details.

Paste the necessary app manifest xml file for providing permissions. Click on create. The following code snippet shows the read permissions for site collection. 
  1. <AppPermissionRequests AllowAppOnlyPolicy="true">  
  2.  <AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="Write" />  
  3. </AppPermissionRequests>  

The write permission level is given to site collection for accessing the data. The scope and write values can be changed for business needs. 

Go back to your node project or re-create the node project as mentioned in my previous article. In the index.js file, create the client id and client secret variables. Paste the Id and secret values generated on the app creation page for authentication. The below code snippet shows the index.js file to access the SharePoint data from Node.JS app. (You could see there are two calls to get the required data. First call to get the OAuth access token using client credentials. Then with the token available on headers set, the required data can be accessed using REST API).
  1. var http = require('http');  
  2. var spauth = require('node-sp-auth');  
  3. var requestprom = require('request-promise');  
  4. // Site and User Creds  
  5. var url = '';  
  6. var clientId = "3714fd27-3592-4772-a91f-6920b70c6b8e";  
  7. var clientSecret = "**************************************";  
  8. var server = http.createServer(function(request, response) {  
  9.     // Authenticate with hardcoded credentials - Get Access Token  
  10.     spauth.getAuth(url, {          
  11.         clientId:clientId,  
  12.         clientSecret:clientSecret  
  13.     })  
  14.     .then(function(options){  
  15.         // Access Token will be available on the options.headers variable
  16.         var headers = options.headers;  
  17.         headers['Accept'] = 'application/json;odata=verbose';  
  18.         // Pull the SharePoint list items  
  19.         requestprom.get({  
  20.         url: url+"/_api/web/lists/getByTitle('customlist1')/items?$select=Title",  
  21.             headers: headers,  
  22.             json: true  
  23.         }).then(function(listresponse){  
  24.             var items = listresponse.d.results;  
  25.             var responseJSON = [];  
  26.             // process  
  28.             // Print / Send back the data  
  29.             response.end(JSON.stringify(listresponse));  
  31.         });  
  32.     });  
  34. });  
  36. var port = process.env.PORT || 1337;  
  37. server.listen(port);  

Deploy/ Run

Test the application by running node index.js command in the command prompt. Open the browser and test it with localhost URL and port number specified 1337. (http://localhost:1337)


Thus, you have learned about accessing the SharePoint data from NodeJS application with access token using app only policy permissions.