Reputation: 37
Is there any way to authenticate an application with SharePoint rest API like Graph API using Client ID and Client Secret? I wanna use SharePoint rest API in my console application.
Upvotes: 0
Views: 1016
Reputation: 20635
This works for me:
In Azure Active Directory I have configured permissions for SharePoint API.
In my code I have scopes defined this way:
var scopes = new [] {"https://<tenantName>.sharepoint.com/allsites.manage"};
var clientApp = ConfidentialClientApplicationBuilder.Create($"{clientId}")
.WithAuthority($"https://login.microsoftonline.com/{tenantId}")
.WithClientSecret($"{clientSecret}").Build();
// acquire a token for the app
AuthenticationResult result = null;
try
{
result = await clientApp.AcquireTokenForClient(scopes)
.ExecuteAsync();
}
catch (MsalUiRequiredException ex)
{
...
}
catch (MsalServiceException ex)
{
...
}
Upvotes: 1