Search code examples
authenticationsharepointsharepoint-2016

SharePoint 2016: Get access token


I need to invoke intranet SharePoint 2016 REST API's to retrieve items. My options are CSOM (or JSOM) or REST APIs. I plan to use REST API's which needs an access token. I've looked up documentation but nothing seems straightforward. Does anybody have suggestions on how I can get access token w/o asking user for credentials each time.

In addition, Im trying to get list of new documents that have been uploaded between subsequent checks. Is there a way to do this? Think of differential updates vs full.


Solution

  • You can get SharePoint 2016 form digest value by doing a POST Request like this:

       $.ajax 
        ({   
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/contextinfo",   
            type: "POST",   
            async: false,   
            headers: { "accept": "application/json;odata=verbose" },   
            success: function(data){   
                var FormDigestValue = data.d.GetContextWebInformation.FormDigestValue; 
                console.log(FormDigestValue);            
            },   
            error: function (xhr, status, error)
            {
                  console.log("Failed");
            }  
        });
    

    enter image description here

    Then use this digest value in ajax request headers when connecting to SharePoint using Rest API: enter image description here

    Reference:

    Get Form Digest Value in SharePoint using REST API

    SharePoint – How to Authenticate SharePoint REST APIs with REST Client