Query sharepoint online api with azureAD user

Landry, Alexandre 1 Reputation point
2022-08-02T17:52:52.127+00:00

I have a sharepoint ( mycompany.sharepoint.com) with basic api/odata activate (mycompany.sharepoint.com/sites/mysite/_api/).
I have some user connecting on excel with power query using their azureAD account. I want to consume the same api in c# with my azureAD account.
I didn't succeed to add the service reference. Any call to the api is forbidden (need a way to connect).
Usually with azure things like storage I use azure.identity but I dont know how to do it with that api.
Did I required some kind of azure data flow?

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,628 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,248 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,021 Reputation points
    2022-08-08T15:06:36.407+00:00

    To connect to online SharePoint, you use a bearer token in the api calls. You use the oauth api to get the token. The easiest way to get this token for ad accounts is the msal library

    https://learn.microsoft.com/en-us/azure/active-directory/develop/msal-overview


  2. RaytheonXie_MSFT 31,071 Reputation points Microsoft Vendor
    2022-08-18T08:52:45.75+00:00

    Hi @Landry, Alexandre ,
    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:

    [Query sharepoint online api with azureAD user]

    Issue Symptom:
    access/consume sharepoint online by calling the rest api, using c#

    Current status:
    Using following code to achieve requirement:

    https://github.com/Azure-Samples/ms-identity-dotnet-desktop-tutorial/tree/master/2-TokenCache
    https://www.c-sharpcorner.com/article/calling-graph-api-via-console-application-using-net-5/

     var redirectUri = "http://localhost";  
     var authority = $"https://login.microsoftonline.com/{tenantId}/v2.0";  
          
     List<string> scopes = new List<string>();  
     scopes.Add("https://graph.microsoft.com/.default");  
          
     var cca = ConfidentialClientApplicationBuilder.Create(clientId)  
                                             .WithAuthority(authority)  
                                             .WithRedirectUri(redirectUri)  
                                             .WithClientSecret(clientSecret)  
                                             .Build();  
     return new MsalAuthenticationProvider(cca, scopes.ToArray());  
          
          
     var client = new GraphServiceClient(authenticationProvider);  
          
     var graphRequest = client.Users.Request();  
          
     var results = graphRequest.GetAsync().Result;  
    

    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!

    0 comments No comments