简体   繁体   中英

How do I use SharePoint Rest API in C# .NET Framework using Client ID and Client Secret?

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.

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)
{
...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM