6

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.

1 Answer 1

3
+100

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

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I not familiar with import, when I add this to the top of my file I get, "Initialization has failed due to: SyntaxError: Unexpected token import", if I change the import to "const request = require('node-sp-auth');" instead I get the error "Initialization has failed due to: Error: Cannot find module 'node-sp-auth'", will this library work with an IBM Cloud Function?
Not at all familiar with IBM cloud function :( , coming from Azure/SharePoint background. But if it supports nodejs, it should work. Your syntax is correct while using require, so not sure whats wrong. I am assuming that you did npm i was done correctly and published as well. Secondly, you can take a look at node-sp-auth-config library wherein the author has used the require syntax. Just ensure that the packages are installed correctly and published in your function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.