Skip to content

SharePoint Online user credentials authentication

Sergei Sergeev edited this page Sep 26, 2018 · 4 revisions

This authentication option uses Microsoft Online Security Token Service https://login.microsoftonline.com/extSTS.srf and SAML tokens in order to obtain authentication cookie. As soon as cookie obtained, you need to attach it to your http request in order to run authenticated queries against SharePoint Online.

Credential options:

  • username - required string, username for SharePoint Online, for example [your user]@[your company].onmicrosoft.com
  • password - required string, password
  • online - optional boolean. You can explicitly specify, that your credentials are for SharePoint Online. It solves some issues with custom domains in SharePoint Onlines. See this issue for additional info.

Example:

{
  username: 'johndoe@contoso.onmicrosoft.com',
  password: 'sd46$fFF$'
}

Resolving object:

{
  headers: {
    'Cookie': '<FedAuth authentication cookie>'
  }
}

Configuration required:

No additional configuration required, since you simply supplying user credentials.

Sample using:

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

spauth
  .getAuth('https://[organization].sharepoint.com/sites/dev/', {
    username: '[user]@[organization].onmicrosoft.com',
    password: '[password]'
  })
  .then(data => {
    var headers = data.headers;
    headers['Accept'] = 'application/json;odata=verbose';

    request.get({
      url: 'https://[organization].sharepoint.com/sites/dev/_api/web',
      headers: headers,
      json: true
    }).then(response => {
      console.log(response.d.Title);
    });
  });