msgraph-sdk-php icon indicating copy to clipboard operation
msgraph-sdk-php copied to clipboard

Logging example would be useful

Open ianef opened this issue 7 months ago • 0 comments

I can't find any practical examples of how to apply logging to graph requests. When things don't work as expected it's really handy to be able to see what you are actually sending and receiving from the API.

I've tried the following but it crashes trying to create the client:

        $this->tokenRequestContext = new AuthorizationCodeContext(
            $this->config['tenant_id'],
            $this->config['app_id'],
            $this->config['client_secret_value'],
            'NOT_AUTHENTICATED', // Auth code dummy value
            $this->urlGenerator->generate('ms365_login_callback', [], UrlGeneratorInterface::ABSOLUTE_URL),
        );

        $tokenCache = new UserAccessTokenCache($this->tokenRequestContext); // Custom cache
        // Create the default handler stack
        $handlerStack = GraphClientFactory::getDefaultHandlerStack();

        // Add the log handler
        $handlerStack->push(Middleware::log(
            $this->logger,
            new \GuzzleHttp\MessageFormatter('{req_headers} - {req_body} - {res_headers} - {res_body}')
        ));

        $adapter = new GraphRequestAdapter(
            GraphPhpLeagueAuthenticationProvider::createWithAccessTokenProvider(
                GraphPhpLeagueAccessTokenProvider::createWithCache(
                    $tokenCache,
                    $this->tokenRequestContext,
                    self::AZURE_SCOPES
                )
            ),
            GraphClientFactory::createWithMiddleware($handlerStack)
        );

        $this->graphServiceClient = GraphServiceClient::createWithRequestAdapter($adapter);

Any help with this would be very handy.

ianef avatar Jul 23 '24 13:07 ianef