How to get access token in SharePoint Online using CSOM and use in Postman or Google Rest client

This SharePoint csom tutorial, I will explain to you, how to get access token in SharePoint Online using csom (client side object model) and how to pass authorization headers in Postman/Google Rest client to query Rest API’s Requests.

If you are new to Postman, you can checkout Access Rest API using Postman in SharePoint Online.

Also read, How to get Azure AD app-only access token and using Microsoft graph Api to interact with Azure Active Directory

Get access token in SharePoint Online

Now, we will see how to create a console application, connect to a SharePoint Online site and get the access token using the SharePoint client side object model.

Open Visual Studio 2017, then click on File -> New Project -> ConsoleApp

Then in the console application add the below dlls from NuGet packages.

  • Office 365 Dev PnP Core component
  • PnP Core Online

Now, write the below csom code, which will give the access token in SharePoint.

using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AccessToken
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "https://mytenant.sharepoint.com";
            string userName = "user@mytenant.onmicrosoft.com";
            string Password = "********";

            var accessToken = GetUserAuthToken(userName, Password);
            Console.Read();
        }
        private static string GetUserAuthToken(string userPrincipal, string Pwd)
        { 
                var authManager = new AuthenticationManager();
                 var context = authManager.GetAzureADCredentialsContext("https://mytenant.sharepoint.com/sites/Test", userPrincipal, Pwd);
                 Console.WriteLine(context.GetAccessToken());
                 return context.GetAccessToken();
        }
    }
}

It will generate the Access token, Copy & paste the same in Postman/Google Rest client to query Rest API’s Requests in SharePoint Online. You can see the fig below:

Get access token in SharePoint Online

You may like following SharePoint CSOM tutorials:

This SharePoint tutorial, we learned how to get the access token in SharePoint Online using csom (client side object model) and how to pass authorization headers in Postman/Google Rest client to query Rest API’s Requests.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

Keerthi Raj

>