Sharepoint 2013 authentication node js
-
08-02-2021 - |
Frage
is there any way authenticate by node js ? i want to use node web api using sharepoint 2013 auth by username and password.after user login i will reach sharepoint lists for crud operations. i used node-sp-auth but these codes doesnt work for me. still i am getting 401 error code.
spauth
.getAuth('http://domain', {
username: 'username',
password: 'pass1',
host: 'domain'
})
.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://domain/_api/web/lists/getbytitle('cari')/Items?";
request.get(requestOpts).then(response => {
console.log(response.d.Title);
})
})
Lösung
Consider sp-request module. It does all authentication for you. You can use REST API without thinking about the auth:
import * as sprequest from 'sp-request';
let spr = sprequest.create(credentialOptions);
spr.get('http://sp2013dev/sites/dev/_api/web/lists/GetByTitle(\'TestList\')')
.then(response => {
console.log('List Id: ' + response.body.d.Id);
})
.catch(err =>{
console.log('Ohhh, something went wrong...');
});