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.
- Navigate to Azure portal.
- Seek for App Registrations. Click on App Registrations as present under.
- Click on New Registration.
- Enter the Identify and click on Register.
- App registered efficiently. Within the left navigation, click on API Permissions.
- Click on Add a permission.
- Choose Microsoft Graph API as proven under.
- Click on Utility Permissions.
- ChooseLearn.All, Listing.Learn.All, Group.ReadWrite.All, Listing.ReadWrite.All permission and click on Add permissions.
- Click on Grant admin consent.
- 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.
- Within the left navigation, click on Certificates & secrets and techniques. Click on New consumer secret.
- Enter the outline and click on Add.
- Copy the key worth which shall be utilized in console utility for authentication.
Create and run the Console Utility
- Open Visual Studio 2019 and create a Console Utility (.Internet Framework).
- Set up the next NuGet Packages both utilizing Bundle Supervisor UI in Visual Studio or the Bundle Supervisor Console.
- Set up-Bundle Microsoft.Graph
- Set up-Bundle Microsoft.Graph.Auth -IncludePrerelease
- Open Program.cs and add the next code.
- utilizing Microsoft.Graph;
- utilizing Microsoft.Graph.Auth;
- utilizing Microsoft.Identification.Consumer;
- utilizing System;
- utilizing System.Collections.Generic;
- utilizing System.Linq;
- utilizing System.Textual content;
- utilizing System.Threading.Duties;
- namespace GraphAPIConsole
- {
- class Program
- {
- static void Primary(string[] args)
- {
- strive
- {
- getUsersAsync().GetAwaiter().GetResult();
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- Console.ReadLine();
- }
- public async static Job getUsersAsync()
- {
- var clientId = “7b1ce1ad-af15-4e5f-9ae4-aaf0a68a7ab4”;
- var tenantId = “e8e6d018-a834-406b-9f43-2e94ae425876”;
- var clientSecret = “2G_tDd4AM92Jv4j_a2hSa0mu05V5ddDr..”;
- IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
- .Create(clientId)
- .WithTenantId(tenantId)
- .WithClientSecret(clientSecret)
- .Construct();
- ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
- GraphServiceClient graphClient = new GraphServiceClient(authProvider);
- var teams = await graphClient.Teams.Request().Choose(x => new { x.Id, x.DisplayName }).GetAsync();
- foreach (var group in teams)
- {
- Console.WriteLine($“{group.DisplayName}, {group.Id}”);
- }
- }
- }
- }
- Hit F5.
Abstract
Thus, on this article, you noticed tips on how to entry Microsoft Graph API in Console Utility.