c# - 如何创建GraphServiceClient?

标签 c# microsoft-graph-api graphserviceclient

我正在使用 Microsoft Graph API 开发 Windows 窗体应用程序。有谁知道如何获取访问 token 以及如何创建 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;
        }

关于c# - 如何创建GraphServiceClient?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66554533/

相关文章:

c# - GraphServiceClient PageIterator 失败,错误为 'The Parsable does not contain a collection property'

c# - 在 Azure 中使用 GraphServiceClient 和客户端 key 时出现授权异常

microsoft-graph-api - 使用 Microsoft Graph API - Java 授予管理员许可

c# - PGP 压缩和加密

c# - 无法在网站项目中找到 System.Net.Http

c# - getter , setter c#

java - Microsoft Graph 放置 api 未在图形客户端中显示为方法

azure - 批准 AAD 租户中的单个应用程序以防止 AADSTS90093 错误

c# - 两个 datagridview 绑定(bind)到同一个表。刷新不起作用