top of page

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 look at 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');  // Site and User Creds

  4. var url = '';  

  5. var clientId = "3714fd27-3592-4772-a91f-6920b70c6b8e";  

  6. var clientSecret = "**************************************";  

  7. var server = http.createServer(function(request, response) {  

  8. // Authenticate with hardcoded credentials - Get Access Token    

  9.  spauth.getAuth(url, {                  

  10. clientId:clientId,          

  11. clientSecret:clientSecret      

  12. })      

  13. .then(function(options){  

  14. // Access Token will be available on the options.headers variable

  15. var headers = options.headers;          

  16. headers['Accept'] = 'application/json;odata=verbose';  

  17. // Pull the SharePoint list items         

  18. requestprom.get({          

  19. url: url+"/_api/web/lists/getByTitle('customlist1')/items?$select=Title",              

  20. headers: headers,             

  21.  json: true         

  22. })

  23. .then(function(listresponse){  

  24. var items = listresponse.d.results;  

  25. var responseJSON = [];  

  26. // process

  27. // Print / Send back the data             

  28. response.end(JSON.stringify(listresponse));           

  29. });      

  30. });  

  31. });  

  32. var port = process.env.PORT || 1337;  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.



bottom of page