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

In 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 = "[email protected]";
            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.

  • Hi Keerthi, thanks for the post. Could you please assist me with few things .
    1) I am unable to generate access token without CSOM, it was generating fine for one of the sharepoint url and then for another it started throwing error : “AADSTS90002: Tenant ****** not found.
    2) Also is there any other way to generate the token without the CSOM.
    currently, I am generating it in following way :
    https://accounts.accesscontrol.windows.net/{tenant_Id}/tokens/OAuth/2

  • Hi Keerthi,
    I am getting the below exception while trying to get the token as you described-
    System.Net.WebException: ‘The remote server returned an error: (401) Unauthorized.’
    But I am able to login and access all the contents over the web on sharepoint online using the same credentials!!! Do I need to have some special right in order to get access token? Please suggest.

  • Hi Raj,

    I am unable to get access token using the CSOM NuGet package. It gives me an exception with this message:
    “Value cannot be null.rnParameter name:value”
    The source is Newtonsoft.Json.

  • >