Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create GraphServiceClient?

I'm developing Windows form app using Microsoft Graph API. Does anyone know how to get an access token and how to create GraphServiceClient?

like image 484
Goga Avatar asked Mar 28 '26 05:03

Goga


1 Answers

The graphClient gets the access token for you, you can also do it yourself using httpClient, but if you do it like this you have to pass the clientSecretCredentials to the graphClient. Mine are stored within the Settings-class.

private readonly GraphServiceClient graphClient;

    internal Service()
    {
        ClientSecretCredential clientSecretCredential = new(
            Settings.TenantId,
            Settings.ClientId,
            Settings.CLientSecret,
            new ClientSecretCredentialOptions() { AuthorityHost = AzureAuthorityHosts.AzurePublicCloud });

        graphClient = new GraphServiceClient(clientSecretCredential, Settings.Scopes);
    }

The settings class looks like this:

internal static class Settings
{
    internal const string ClientId = "my_client_id";
    internal const string TenantId = "my_tenant_id";
    internal const string CLientSecret = "my_client_secret";
    internal static string[] Scopes = { "https://graph.microsoft.com/.default" };
}
like image 115
Fabian Avatar answered Mar 29 '26 19:03

Fabian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!