【问题标题】:How to create GraphServiceClient?如何创建 GraphServiceClient?
【发布时间】:2021-03-09 20:46:09
【问题描述】:

我正在使用 Microsoft Graph API 开发 Windows 窗体应用程序。有谁知道如何获取访问令牌以及如何创建 GraphServiceClient?

【问题讨论】:

    标签: microsoft-graph-api graphserviceclient


    【解决方案1】:

    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;
            }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-19
      • 2023-01-27
      • 1970-01-01
      • 1970-01-01
      • 2019-09-05
      • 2021-01-19
      • 1970-01-01
      • 2022-01-19
      相关资源
      最近更新 更多