Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Sharepoint document from Node.js in IBM Cloud Function

I'm trying to access some table data in a Sharepoint file hosted on botshop.cloudappsportal.com from an IBM Cloud Function written in Node.js.

I can access the REST API for Sharepoint on the site, but the authentication fails. My understanding, is that Sharepoint uses some complicated Microsoft authentication.

Here is the Node.js code,

const request = require('request-promise');

function main(params) {
    var options = {
      uri: "http://xxx.botshop.cloudappsportal.com/_api/web/lists/getbytitle('myfile')/items",
      method: 'GET',
      auth: {
        'user': '[email protected]',
        'pass': 'password'
      }
    }
    return request.get(options).then(response => {
        return response;
    });
}

exports.main = main;

I can access the URL from a browser, after it prompts for user/password.

I was also able to access it from Java, using the NTCredentials class.

HttpGet request = new HttpGet("http://xxx.botshop.cloudappsportal.com/_api/web/lists/getbytitle('myfile')/items");
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 1000);
HttpConnectionParams.setSoTimeout(httpParams, 1000);
DefaultHttpClient client = new DefaultHttpClient(httpParams);
client.getCredentialsProvider().setCredentials(
    new AuthScope(AuthScope.ANY),
    new NTCredentials("[email protected]", "password", "", ""));
HttpResponse response = client.execute(request);

The normal UsernamePasswordCredentials did not work in Java, so I need the equivalent of NTCredentials in Node.js (that works inside IBM Cloud Functions). Any ideas?

Also somewhat odd that the extra arguments of the domain just "" "" works, so odd it needs the NTCredentials when it does not pass any additional useful data.

like image 578
James Avatar asked Aug 23 '18 19:08

James


1 Answers

You can make use of the excellent node-sp-auth library to authenticate to SharePoint with nodejs via different ways.

It supports authentication via user credentials. It uses http ntlm handshake in order to obtain authentication header.

For example:

import * as spauth from 'node-sp-auth';
import * as request from 'request-promise';

spauth
  .getAuth('http://xxx.botshop.cloudappsportal.com/', {
    username: 'administrator',
    password: '[password]',
    domain: 'sp'
  })
  .then(data =>{
    let headers = data.headers;
    headers['Accept'] = 'application/json;odata=verbose';
    let requestOpts = data.options;
    requestOpts.json = true;
    requestOpts.headers = headers;
    requestOpts.url = 'http://xxx.botshop.cloudappsportal.com/_api/web/lists/getbytitle('myfile')/items';

    request.get(requestOpts).then(response => {
      console.log(response.d.Title);
    });
});

References - node-sp-auth - nodejs to SharePoint unattended http authentication

SharePoint on premise user credentials authentication

Also, if you dont want to authenticate via username/password, you can make use of client-id and secret and then use them to authenticate to SharePoint as mentioned in the below link. While it is written for SPO, it should also work with On Premise environment as well when you follow similar steps

SharePoint Online addin only authentication

like image 178
Gautam Sheth Avatar answered Oct 15 '22 06:10

Gautam Sheth