I'm developing Windows form app using Microsoft Graph API. Does anyone know how to get an access token and how to create GraphServiceClient?
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" };
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With