How To Access Microsoft Graph API In Console Application

Microsoft Graph .Net SDK is available and it can be included in the Console Application by installing NuGet packages.
  • Graph
  • Graph.Beta
  • Graph.Core
  • Graph.Auth
Click here to learn more about Microsoft Graph SDK available for other platforms and languages.
 

Auth Provider Instance

 
For this console application, I have used Client credentials provider. The client credential flow enables service applications to run without user interaction. Access is based on the identity of the application. 
 
Click here to choose a Microsoft Graph authentication provider based on scenario.
 

List Groups Graph API - Get all the groups using Graph API

 
 
Permissions required
  • Permission Type – Application
  • Permissions - Group.Read.All, Directory.Read.All, Group.ReadWrite.All, Directory.ReadWrite.All
In this article, you will see how to perform the following tasks,
  • Register an Application in Azure - Register an app in Azure AD and add the required permissions to access the Graph API
  • Create and run the console application

Register an application in Azure

 
Register an application in Azure AD to access the Graph API.
  1. Navigate to Azure portal.
  2. Search for App Registrations. Click App Registrations as show below.

    How To Access Microsoft Graph API In Console Application
  1. Click New Registration.

    How To Access Microsoft Graph API In Console Application
  1. Enter the Name and click Register.

    How To Access Microsoft Graph API In Console Application
  1. App registered successfully. In the left navigation, click API Permissions.

    How To Access Microsoft Graph API In Console Application
  1. Click Add a permission.

    How To Access Microsoft Graph API In Console Application
  1. Select Microsoft Graph API as shown below.

    How To Access Microsoft Graph API In Console Application
  1. Click Application Permissions.

    How To Access Microsoft Graph API In Console Application
  1. SelectRead.All, Directory.Read.All, Group.ReadWrite.All, Directory.ReadWrite.All permission and click Add permissions.

    How To Access Microsoft Graph API In Console Application
  1. Click Grant admin consent.

    How To Access Microsoft Graph API In Console Application

    How To Access Microsoft Graph API In Console Application
  1. In the left navigation, click Overview. Copy the Application (client) ID and Directory (tenant) ID values. These values will be used in the console application for authentication.

    How To Access Microsoft Graph API In Console Application
  1. In the left navigation, click Certificates & secrets. Click New client secret.

    How To Access Microsoft Graph API In Console Application
  1. Enter the description and click Add.

    How To Access Microsoft Graph API In Console Application
  1. Copy the secret value which will be used in console application for authentication.

    How To Access Microsoft Graph API In Console Application

Create and run the Console Application

  1. Open Visual Studio 2019 and create a Console Application (.Net Framework).
  2. Install the following NuGet Packages either using Package Manager UI in Visual Studio or the Package Manager Console.
    1. Install-Package Microsoft.Graph  
    2. Install-Package Microsoft.Graph.Auth -IncludePrerelease  
  1. Open Program.cs and add the following code.
    1. using Microsoft.Graph;  
    2. using Microsoft.Graph.Auth;  
    3. using Microsoft.Identity.Client;  
    4. using System;  
    5. using System.Collections.Generic;  
    6. using System.Linq;  
    7. using System.Text;  
    8. using System.Threading.Tasks;  
    9.   
    10. namespace GraphAPIConsole  
    11. {  
    12.     class Program  
    13.     {  
    14.         static void Main(string[] args)  
    15.         {  
    16.             try  
    17.             {  
    18.                 getUsersAsync().GetAwaiter().GetResult();  
    19.             }  
    20.             catch (Exception ex)  
    21.             {  
    22.                 Console.WriteLine(ex.Message);  
    23.             }  
    24.             Console.ReadLine();  
    25.         }  
    26.   
    27.         public async static Task getUsersAsync()  
    28.         {  
    29.             var clientId = "7b1ce1ad-af15-4e5f-9ae4-aaf0a68a7ab4";  
    30.             var tenantId = "e8e6d018-a834-406b-9f43-2e94ae425876";  
    31.             var clientSecret = "2G_tDd4AM92Jv4j_a2hSa0mu05V5ddDr..";  
    32.             IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder  
    33.                 .Create(clientId)  
    34.                 .WithTenantId(tenantId)  
    35.                 .WithClientSecret(clientSecret)  
    36.                 .Build();  
    37.   
    38.             ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);  
    39.             GraphServiceClient graphClient = new GraphServiceClient(authProvider);  
    40.   
    41.             var groups = await graphClient.Groups.Request().Select(x => new { x.Id, x.DisplayName }).GetAsync();  
    42.             foreach (var group in groups)  
    43.             {  
    44.                 Console.WriteLine($"{group.DisplayName}, {group.Id}");  
    45.             }  
    46.         }  
    47.     }  
    48. }  
  1. Hit F5.

    How To Access Microsoft Graph API In Console Application

Summary

 
Thus, in this article, you saw how to access Microsoft Graph API in Console Application.