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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With