23 lines
1.1 KiB
C#
23 lines
1.1 KiB
C#
using Azure.Identity;
|
|
using Microsoft.Extensions.Options;
|
|
using Microsoft.Graph;
|
|
|
|
namespace SIFinance.Api;
|
|
|
|
// Construit le GraphServiceClient à la demande plutôt qu'au démarrage : tant que les
|
|
// identifiants SharePoint (TenantId/ClientId/ClientSecret) ne sont pas renseignés,
|
|
// ClientSecretCredential lève une exception à la construction — on veut que ça arrive
|
|
// à l'intérieur du try/catch de l'endpoint qui l'appelle, pas pendant la résolution DI.
|
|
public class SharePointGraphClientFactory(IOptions<SharePointOptions> options)
|
|
{
|
|
public GraphServiceClient Create()
|
|
{
|
|
var opts = options.Value;
|
|
if (string.IsNullOrWhiteSpace(opts.TenantId) || string.IsNullOrWhiteSpace(opts.ClientId) || string.IsNullOrWhiteSpace(opts.ClientSecret))
|
|
throw new InvalidOperationException("Configuration SharePoint incomplète (TenantId/ClientId/ClientSecret) — voir SIFinance.Api/appsettings.Development.json.");
|
|
|
|
var credential = new ClientSecretCredential(opts.TenantId, opts.ClientId, opts.ClientSecret);
|
|
return new GraphServiceClient(credential, ["https://graph.microsoft.com/.default"]);
|
|
}
|
|
}
|