Introduction
In this article, you will learn how to integrate SharePoint applications data into node applications with app authentication approach.
Scenario
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.
- <AppPermissionRequests AllowAppOnlyPolicy="true">
- <AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="Write" />
- </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).
- var http = require('http');
- var spauth = require('node-sp-auth');
- var requestprom = require('request-promise');
-
- var url = 'https://nakkeerann.sharepoint.com';
- var clientId = "3714fd27-3592-4772-a91f-6920b70c6b8e";
- var clientSecret = "**************************************";
- var server = http.createServer(function(request, response) {
-
- spauth.getAuth(url, {
- clientId:clientId,
- clientSecret:clientSecret
- })
- .then(function(options){
-
- var headers = options.headers;
- headers['Accept'] = 'application/json;odata=verbose';
-
- requestprom.get({
- url: url+"/_api/web/lists/getByTitle('customlist1')/items?$select=Title",
- headers: headers,
- json: true
- }).then(function(listresponse){
- var items = listresponse.d.results;
- var responseJSON = [];
-
-
-
- response.end(JSON.stringify(listresponse));
-
- });
- });
-
- });
-
- 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)
Summary
Thus, you have learned about accessing the SharePoint data from NodeJS application with access token using app only policy permissions.