Home > Software engineering >  Microsoft.Graph C#: Make an API request programmatically
Microsoft.Graph C#: Make an API request programmatically

Time:10-28

I'm working with Microsoft.Graph SDK and I need to get email SentItems programmatically in a class library.

I'm using the following code to create a client:

private static Graph.GraphServiceClient CreateClient()
{
    var scopes = new[] { "User.Read" };

    // Multi-tenant apps can use "common",
    // single-tenant apps must use the tenant ID from the Azure portal
    var tenantId = "xxx";

    // Value from app registration
    var clientId = "xxxx";

    var pca = Microsoft.Identity.Client.PublicClientApplicationBuilder
        .Create(clientId)
        .WithTenantId(tenantId)
        .Build();

    // DelegateAuthenticationProvider is a simple auth provider implementation
    // that allows you to define an async function to retrieve a token
    // Alternatively, you can create a class that implements IAuthenticationProvider
    // for more complex scenarios
    var authProvider = new Graph.DelegateAuthenticationProvider(async (request) =>
    {
        // Use Microsoft.Identity.Client to retrieve token
        var result = await pca.AcquireTokenByIntegratedWindowsAuth(scopes).ExecuteAsync();

        request.Headers.Authorization =
            new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", result.AccessToken);
    });

    return new Graph.GraphServiceClient(authProvider);
}

then I'm trying to use the client the next waay:

var sentEmails = graphClient.Users[authMail].MailFolders.SentItems.Request().GetAsync().Result;

but I'm getting the following exception when executing the request:

Exception thrown: 'Microsoft.Identity.Client.MsalUiRequiredException' in System.Private.CoreLib.dll Exception thrown: 'System.AggregateException' in System.Private.CoreLib.dll

I thought that another option could be to get an auth token. I can get an auth token with the next code:

private static async Task<string> GetGraphToken()
{
    var resource = "https://graph.microsoft.com/";
    var instance = "https://login.microsoftonline.com/";
    var tenant = "xxx";
    var clientID = "xxxx";
    var secret = "xxxxx";
    var authority = $"{instance}{tenant}";
    var authContext = new AuthenticationContext(authority);
    var credentials = new ClientCredential(clientID, secret);
    var authResult = authContext.AcquireTokenAsync(resource, credentials).Result;
    return authResult.AccessToken;
}

And it just works, but then I don't know how to use it to do an API request programmatically.

Any of the two variantes is OK for me, getting rid of the exceptions in the first case, or finding a way to use the token to make a programmatic SDK API call in the second.

Any help?

Edit 1:

I'm trying with the next approach, but the same exception is thrown:

var accessToken = GetToken();

var client = new Graph.GraphServiceClient(
    new Graph.DelegateAuthenticationProvider(
        (requestMessage) =>
        {
            requestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
            return Task.FromResult(0);
        }));
        
var mails = client.Users[authMail].MailFolders.SentItems.Messages.Request().GetAsync().Result;

CodePudding user response:

In your first example you trying to use IWA auth enter image description here

enter image description here

Pls consent the api permission and try code below:

using Microsoft.Graph;
using Azure.Identity;

var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "tenant_name.onmicrosoft.com";
var clientId = "aad_app_id";
var clientSecret = "client_secret";
var clientSecretCredential = new ClientSecretCredential(
                tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var res = await graphClient.Users["{user-id}"].MailFolders.SentItems.Request().GetAsync();
  • Related