azure - 通过 AAD 调用 SharePoint REST API 的访问令牌?
问题描述
我目前正在构建一个 .NET Core 应用程序,它对以下内容执行直接 SharePoint REST 调用:contoso.sharepoint.com/sites/shipment/_api/search/query?querytext='...'
.NET Core 应用程序在应用程序注册中注册。如何检索访问令牌?(由于某种原因,MS Graph API 无法进行这些调用,因此尝试使用 SPO REST API)
解决方案
您可以使用证书方式获取令牌,如下所示:
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;
}
推荐阅读
- php - 当我运行项目时,它显示控制器没有在 laravel 中退出,但它在控制器文件夹中我该怎么办?
- reactjs - 反应 SWR 获取未定义
- angular - 基类调用 Subscription 中的抽象方法
- javascript - Angular Universal 9 打破了 Bootstrap CSS 指令
- php - PHP POST 没有正确重定向
- r - 使用 R 从包含关键字的 PDF 中提取段落
- oracle - 如何避免在 Oracle 中具有 numberAlphabet 模式的列的列名中的 select 语句中出现“”?
- php - PHP mysqli查询搜索反斜杠
- user-interface - 在 Superset 中,用于共享的 URL 生成为“127.0.0.1:8088”而不是实际域,除了 SQL Lab 中的“共享查询”按钮
- spring-boot - 我们可以在 AWS Lambda 上部署 Spring 微服务吗?