SharePoint 2016: Get access token

developer sp1 461 Reputation points
2020-10-05T04:44:56.867+00:00

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.

Microsoft 365 and Office | SharePoint Server | Development
0 comments No comments
{count} votes

Accepted answer
  1. ZhengyuGuo 10,586 Reputation points Moderator
    2020-10-05T06:00:37.477+00:00

    Hi @developer sp1 ,

    Please do a Post Request to /_api/contextinfo to get SharePoint form digest token for authentication:

    $.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");
    }
    });

    30084-1.png

    29999-2.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.