从客户端获取承载或 AuthResult?

Getting bearer or AuthResult from client?

我正在像这样实例化图形:

public static async Task<GraphServiceClient> GetGraphClient(string clientId, string clientSecret, string tenantName)
{
    AuthenticationResult authResult = await
       GetAuthenticationResult(tenantName, clientId, clientSecret);

    var client = new GraphServiceClient(new DelegateAuthenticationProvider(
        (requestMessage) =>
        {
            requestMessage.Headers
                .Add("Authorization", "Bearer " + authResult.AccessToken);
            return Task.FromResult(0);

        }));

    return client;
}

我有另一种扩展方法,我需要来自客户端的 Bearer:

public static async Task<T> GetRESTResult<T>(this GraphServiceClient client, string url)
{
    HttpClient http = new HttpClient();

    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
    request.Headers.Authorization =
        new AuthenticationHeaderValue("Bearer", HERE????.AccessToken);
    HttpResponseMessage response = await http.SendAsync(request);
    var jsonStr = response.Content.ReadAsStringAsync();
    return JsonConvert.DeserializeObject<T>(jsonStr.Result);
}

如何从给定的客户端对象中获取 authresult/bearer?

与其从 SDK 中提取令牌 ,不如 send the HTTP requests through the SDK:

public static async Task<T> GetRESTResult<T>(this GraphServiceClient client, string url)
{
    // Create the request message and add the content.
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);

    // Authenticate (add access token) our HttpRequestMessage
    await client.AuthenticationProvider.AuthenticateRequestAsync(request);

    // Send the request and get the response.
    HttpResponseMessage response = await client.HttpProvider.SendAsync(request);

    // Process the response
    var jsonStr = response.Content.ReadAsStringAsync();
    return JsonConvert.DeserializeObject<T>(jsonStr.Result);
}