-
Notifications
You must be signed in to change notification settings - Fork 63
Description
I'm trying to upgrade to v5 of the .NET SDK. In v4 we have a handler that logs the HttpRequestMessage.
The code is slightly changed in v5 in comparison with v4.
I now following the documentation in the repo and following the documentation in the Microsoft docs.
I think the documentation of Microsoft docs is better because the documentation here in the repository uses an overload of GraphClientFactory.CreateDefaultHandlers(authenticationProvider); that doesn't exist anymore.
So I'm following the Microsoft docs with the following code:
// https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options);
var authProvider = new AzureIdentityAuthenticationProvider(clientSecretCredential, scopes);
var handlers = GraphClientFactory.CreateDefaultHandlers();
// Add logging handler before the compression handler. Compression handler is created by CreateDefaultHandlers()
HttpRequestMessageLoggingHandler loggingHandler = new();
handlers.Insert(0, loggingHandler);
var httpClient = GraphClientFactory.Create(handlers);
var graphClient = new Microsoft.Graph.Beta.GraphServiceClient(httpClient, authProvider);Simple logging handler:
public class HttpRequestMessageLoggingHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage httpRequest, CancellationToken cancellationToken)
{
// log the request before it goes out.
Console.WriteLine($"Sending request => method: '{httpRequest.Method.Method}', uri: '{httpRequest.RequestUri?.AbsoluteUri}'");
// Always call base.SendAsync so that the request is forwarded through the pipeline.
HttpResponseMessage httpResponse = await base.SendAsync(httpRequest, cancellationToken);
// log the response as it comes back.
Console.WriteLine(
$"Received response => httpStatusCode: {(int)httpResponse.StatusCode}, reason: '{httpResponse.ReasonPhrase}'");
return httpResponse;
}
}Expected behavior
Getting log of the request through my HttpRequestMessageLoggingHandler and result of the call that I make to the MS Graph.
Actual behavior
- Documentation is not up to date because of missing overload
GraphClientFactory.CreateDefaultHandlers(authenticationProvider);. - Getting 401, InvalidAuthenticationToken, Access token is empty. message with above code.
I think the Authprovider not used/set???? Followed this documentation of the Microsoft Docs.
Steps to reproduce the behavior
Follow above code to create graph service client and make a call to an endpoint. You will get a 401.
User? user = await graphClient.Me.GetAsync();