How to connect and use the Microsoft Graph API?

microsoft_graphIn this article, I would like to give you some examples of how to connect and use the Microsoft Graph API. My task was to extract contacts from the Global Address List, and unfortunately, this particular method is still not implemented, and only accessible in beta mode. We will go through a technique which will help you to use Microsoft Graph API beta from the stable SDK library and extract the information you might need.

First of all, lets set up the connection. To extract data, I will use the standard console application together with azure application registration. So First of all, open your Azure portal, then go to Azure active directory, then app registrations and create a new app. I’ve created a single-tenant app. Now on the app page, you already have your tenant id and client id, which you will need to connect. One more thing which you also need to generate is client secret, so go to the appropriate section and create the secret. Once all steps are done, the last step in the portal is to give proper permissions. I gave the following permissions to my application.


Usually, you can find all the credentials you might need on the page of the corresponding API call. As an example to list organizational contacts, you need the following credentials.


More information you can find on the API call page: List orgContacts.

The next step is to connect all needed NuGet packages. Most probably, you will need the following packages.


Now we ready to write some code and access Graph APIs.

using System;
using System.Net.Http.Headers;
using Microsoft.Graph;
using Microsoft.Identity.Client;
namespace GraphAPI {
    class Program {
        static void Main(string[] args) {
            var app = ConfidentialClientApplicationBuilder.Create("#your_clinet_id#").WithClientSecret("#your_clinet_secret#").WithRedirectUri("http://localhost").WithAuthority("").Build();
            string[] scopes = new string[] {
            var token = app.AcquireTokenForClient(scopes).ExecuteAsync().Result;
            GraphServiceClient graphClient = new GraphServiceClient("", new DelegateAuthenticationProvider(async(requestMessage) = >{
                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token.AccessToken);
                        //Call some standard api methods 
            var users = graphClient.Users.Request().Top(99).GetAsync().Result;
            var organization = graphClient.Organization.Request().GetAsync().Result; //Call contacts method from beta path 
            IUserContactsCollectionRequestBuilder Contacts = new UserContactsCollectionRequestBuilder(graphClient.BaseUrl.Replace("v1.0", "beta") + "/contacts", graphClient);
            var orgContacts = Contacts.Request().Top(99).GetAsync().Result;

Replace placeholders with the values which you can find in your app registration. In the code above, you can also find some examples of how to call different methods from the standard library as well as from the beta library.

Leave a Reply

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