Azure

How To Entry Microsoft Graph API In Console Utility

Microsoft Graph .Internet SDK is on the market and it may be included within the Console Utility by putting in NuGet packages.

  • Graph
  • Graph.Beta
  • Graph.Core
  • Graph.Auth
Click on right here to be taught extra about Microsoft Graph SDK accessible for different platforms and languages.

 

Auth Supplier Occasion

 

For this console utility, I’ve used Consumer credentials supplier. The consumer credential move permits service functions to run with out person interplay. Entry is predicated on the identification of the appliance. 

 

Click on right here to decide on a Microsoft Graph authentication supplier based mostly on situation.

 

Listing Teams Graph API – Get all of the teams utilizing Graph API

 

 

Permissions required

  • Permission Kind – Utility
  • Permissions – Group.Learn.All, Listing.Learn.All, Group.ReadWrite.All, Listing.ReadWrite.All

On this article, you will notice tips on how to carry out the next duties,

  • Register an Utility in Azure – Register an app in Azure AD and add the required permissions to entry the Graph API
  • Create and run the console utility

Register an utility in Azure

 

Register an utility in Azure AD to entry the Graph API.

  1. Navigate to Azure portal.
  2. Seek for App Registrations. Click on App Registrations as present under.

  1. Click on New Registration.

    How To Access Microsoft Graph API In Console Application

  1. Enter the Identify and click on Register.

    How To Access Microsoft Graph API In Console Application

  1. App registered efficiently. Within the left navigation, click on API Permissions.

    How To Access Microsoft Graph API In Console Application

  1. Click on Add a permission.

    How To Access Microsoft Graph API In Console Application

  1. Choose Microsoft Graph API as proven under.

    How To Access Microsoft Graph API In Console Application

  1. Click on Utility Permissions.

    How To Access Microsoft Graph API In Console Application

  1. ChooseLearn.All, Listing.Learn.All, Group.ReadWrite.All, Listing.ReadWrite.All permission and click on Add permissions.

    How To Access Microsoft Graph API In Console Application

  1. Click on Grant admin consent.

    How To Access Microsoft Graph API In Console Application

    How To Access Microsoft Graph API In Console Application

  1. Within the left navigation, click on Overview. Copy the Utility (consumer) ID and Listing (tenant) ID values. These values shall be used within the console utility for authentication.

    How To Access Microsoft Graph API In Console Application

  1. Within the left navigation, click on Certificates & secrets and techniques. Click on New consumer secret.

    How To Access Microsoft Graph API In Console Application

  1. Enter the outline and click on Add.

    How To Access Microsoft Graph API In Console Application

  1. Copy the key worth which shall be utilized in console utility for authentication.

    How To Access Microsoft Graph API In Console Application

Create and run the Console Utility

  1. Open Visual Studio 2019 and create a Console Utility (.Internet Framework).
  2. Set up the next NuGet Packages both utilizing Bundle Supervisor UI in Visual Studio or the Bundle Supervisor Console.
    1. Set up-Bundle Microsoft.Graph  
    2. Set up-Bundle Microsoft.Graph.Auth -IncludePrerelease  
  1. Open Program.cs and add the next code.
    1. utilizing Microsoft.Graph;  
    2. utilizing Microsoft.Graph.Auth;  
    3. utilizing Microsoft.Identification.Consumer;  
    4. utilizing System;  
    5. utilizing System.Collections.Generic;  
    6. utilizing System.Linq;  
    7. utilizing System.Textual content;  
    8. utilizing System.Threading.Duties;  
    9.   
    10. namespace GraphAPIConsole  
    11. {  
    12.     class Program  
    13.     {  
    14.         static void Primary(string[] args)  
    15.         {  
    16.             strive  
    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 Job 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.                 .Construct();  
    37.   
    38.             ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);  
    39.             GraphServiceClient graphClient = new GraphServiceClient(authProvider);  
    40.   
    41.             var teams = await graphClient.Teams.Request().Choose(x => new { x.Id, x.DisplayName }).GetAsync();  
    42.             foreach (var group in teams)  
    43.             {  
    44.                 Console.WriteLine($“{group.DisplayName}, {group.Id}”);  
    45.             }  
    46.         }  
    47.     }  
    48. }  
  1. Hit F5.

    How To Access Microsoft Graph API In Console Application

Abstract

 

Thus, on this article, you noticed tips on how to entry Microsoft Graph API in Console Utility.

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button