azure - 通过 AAD 进行 SharePoint REST API 调用的访问 token ?

标签 azure .net-core sharepoint azure-active-directory microsoft-graph-api

我目前正在构建一个 .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/

相关文章:

azure - 如何在 Power Query 中使用日志解析器(或其他)转换 IIS 日志?

Azure 逻辑应用当前时间

sql-server - 使用 SSMS 将 CSV 导入 SQL Azure 数据库

c# - Service Fabric Web Api 具有外部和内部端点

c# - 如何从 .NET Core 中的流加载程序集

c# - LINQ OrderBy 在 Visual Studio 立即窗口中的行为与在代码中的行为不同

javascript - 在 DIV 中显示来自 SharePoint 的 CAML 查询结果

wcf - .Net Core 不支持 WSHttpBinding

sharepoint - 使用 PowerShell 在 SharePoint 中检索已部署解决方案的详细信息

sharepoint - SharePoint Online-默认情况下启用网站功能