我目前正在构建一个 .NET Core 应用程序,该应用程序执行直接 SharePoint REST 调用:contoso.sharepoint.com/sites/shipment/_api/search/query?querytext='...'
.NET Core 应用程序已在应用程序注册中注册。如何检索访问 token ? (由于某种原因,MS Graph API 无法进行这些调用,因此尝试 SPO REST API)
最佳答案
您可以使用证书方式来获取 token ,如下所示:
private static async Task<string> GetToken()
{
string applicationId = "xxx";
string tenantId = "contoso.onmicrosoft.com";
X509Certificate2 certificate = new X509Certificate2(@"C:\certificate.pfx", "password");
IConfidentialClientApplication confApp = ConfidentialClientApplicationBuilder.Create(applicationId)
.WithAuthority($"https://login.microsoftonline.com/{tenantId}")
.WithCertificate(certificate) // This is just a local method that gets the certificate on my machine
.Build();
var scopes = new[] { "https://contoso.sharepoint.com/.default" };
var authenticationResult = await confApp.AcquireTokenForClient(scopes).ExecuteAsync();
return authenticationResult.AccessToken;
}
关于azure - 通过 AAD 进行 SharePoint REST API 调用的访问 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69195881/