【发布时间】:2021-03-09 20:46:09
【问题描述】:
我正在使用 Microsoft Graph API 开发 Windows 窗体应用程序。有谁知道如何获取访问令牌以及如何创建 GraphServiceClient?
【问题讨论】:
标签: microsoft-graph-api graphserviceclient
我正在使用 Microsoft Graph API 开发 Windows 窗体应用程序。有谁知道如何获取访问令牌以及如何创建 GraphServiceClient?
【问题讨论】:
标签: microsoft-graph-api graphserviceclient
private async Task<string> GetAccessToken()
{
IConfidentialClientApplication app = ConfidentialClientApplicationBuilder.Create(APP_ID)
.WithClientSecret(APP_SECRET)
.WithAuthority($"https://login.microsoftonline.com/{TENANT_ID}")
.WithRedirectUri("https://localhost")
.Build();
string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
var result = await app.AcquireTokenForClient(scopes).ExecuteAsync();
return result.AccessToken;
}
private GraphServiceClient GetGraphClient()
{
var graphClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) => {
// get an access token for Graph
var accessToken = GetAccessToken().Result;
requestMessage
.Headers
.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
return Task.FromResult(0);
}));
return graphClient;
}
【讨论】: