Call SharePoint Online rest api from console c# app with modern auth

Dinesh Negi 86 Reputation points
2022-04-07T06:54:54.58+00:00

Hi ,

1) I am trying to call sharepoint search api from console app, in tenant only Modern authentication is active.
So i created Azure App and give there api permission ("AllSites.Read", "Sites.Search.All","Sites.Read.All", "Files.Read.All").
2) I am able to get auth token , but getting unauthorized error during api call , below is my code

           var defaultRequestHeaders = HttpClient.DefaultRequestHeaders;
           if (defaultRequestHeaders.Accept == null || !defaultRequestHeaders.Accept.Any(m => m.MediaType == 
               "application/json"))
            {
                HttpClient.DefaultRequestHeaders.Accept.Add( 
                  MediaTypeWithQualityHeaderValue.Parse("application/json;odata=verbose"));
            }
            defaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
            HttpResponseMessage response = await HttpClient.GetAsync(webApiUrl);
            if (response.IsSuccessStatusCode)
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,599 questions
{count} votes

Accepted answer
  1. Tong Zhang_MSFT 9,116 Reputation points
    2022-04-08T09:25:22.783+00:00

    Hi @Dinesh Negi ,
    I'm glad to hear you solve the problem ,if you have any issue about SharePoint, you are welcome to raise a ticket in this forum.

    By the way, since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others." and according to the scenario introduced here: Answering your own questions on Microsoft Q&A, I would make a brief summary of this thread:

    [Call SharePoint Online rest api from console c# app with modern auth]

    Issue Symptom:

    1. I am trying to call sharepoint search api from console app, in tenant only Modern authentication is active.
      So i created Azure App and give there api permission ("AllSites.Read", "Sites.Search.All","Sites.Read.All", "Files.Read.All").
    2. I am able to get auth token , but getting unauthorized error during api call

    Current status:
    After nuget upgraded with Microsoft.identiy.model version verion v 4.4.1. Now able to fix this issue.

    You could click the "Accept Answer" button for this summary to close this thread, and this can make it easier for other community member's to see the useful information when reading this thread. Thanks for your understanding!


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    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.


    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Tong Zhang_MSFT 9,116 Reputation points
    2022-04-08T08:34:06.887+00:00

    Hi @Dinesh Negi ,
    Thanks for your detailed case description.
    According to my research and testing, please check whether the Client ID and Secret are correct, or the token is correct and check the requested URL. They usually result in an unauthorized error.

    Here is a document about how to call SharePoint Online REST API from console application. You can refer to.

    Reference:
    https://www.c-sharpcorner.com/article/consume-sharepoint-online-csom-rest-api-with-dotnet-core-3-1/

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    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.


    0 comments No comments

  2. Dinesh Negi 86 Reputation points
    2022-04-08T09:09:32.423+00:00

    Hi @Tong Zhang_MSFT ,

    As i said , i was able to get access token, so Client ID and Secret should be correct.

    After nuget upgraded with Microsoft.identiy.model version verion v 4.4.1

    Now able to fix this issue.

    Thanks, For your help and research.

    0 comments No comments