kamermans/guzzle-oauth2-subscriber

Argument 1 passed to kamermans\OAuth2\OAuth2Middleware::__invoke() must be callable, object given, called in /var/www/html/vendor/kamermans/guzzle-oauth2-subscriber/src/OAuth2Middleware.php on line 32

bernhardberger opened this issue · 4 comments

public function initializeObject()
    {
        $this->client = new Client([
            'handler' => $this->getHandlerStack(),
            'auth' => 'oauth',
            'base_uri' => self::BASE_URI,
            'verify' => false,
        ]);
    }

    protected function getHandlerStack()
    {
        $oAuth2Client = new Client([
            'base_uri' => 'https://baseurl.tld/oauth/token?grant_type=client_credentials',
            'verify' => false
        ]);

        $clientCredentials = new ClientCredentials($oAuth2Client, [
            'client_id' => 'myclientid',
            'client_secret' => 'myclientsecret'
        ]);

        $oauth2Middleware = new OAuth2Middleware($clientCredentials);

        $handlerStack = HandlerStack::create($oauth2Middleware);
        $handlerStack->push($oauth2Middleware);

        return $handlerStack;
    }

   public function getFoo()
    {
        return json_decode(
            $this->client->get('/v1/foo')->getBody()->__toString(),
            true
        );
    }

Throws the following exception:

Argument 1 passed to kamermans\OAuth2\OAuth2Middleware::__invoke() must be callable, object given, called in /var/www/html/vendor/kamermans/guzzle-oauth2-subscriber/src/OAuth2Middleware.php on line 32
composer show

kamermans/guzzle-oauth2-subscriber v1.0.3                OAuth 2.0 client for Guzzle 4, 5 and 6+
psr/http-message                   1.0.1                 Common interface for HTTP messages
psr/log                            1.0.2                 Common interface for logging libraries
guzzlehttp/guzzle                  6.3.0                 Guzzle is a PHP HTTP client library
guzzlehttp/promises                v1.3.1                Guzzle promises library
guzzlehttp/psr7                    1.4.2                 PSR-7 message implementation that also provides common utility methods

Thanks for the report, @bernhardberger, I'm looking into it now!

Do you have more of the stack trace so I can see where in Guzzle the middleware is being called from?

The problem is here:

$handlerStack = HandlerStack::create($oauth2Middleware);
$handlerStack->push($oauth2Middleware);

You should not pass the middleware to HandlerStack::create() - here is the corrected code:

public function initializeObject()
    {
        $this->client = new Client([
            'handler' => $this->getHandlerStack(),
            'auth' => 'oauth',
            'base_uri' => self::BASE_URI,
            'verify' => false,
        ]);
    }

    protected function getHandlerStack()
    {
        $oAuth2Client = new Client([
            'base_uri' => 'https://baseurl.tld/oauth/token?grant_type=client_credentials',
            'verify' => false
        ]);

        $clientCredentials = new ClientCredentials($oAuth2Client, [
            'client_id' => 'myclientid',
            'client_secret' => 'myclientsecret'
        ]);

        $oauth2Middleware = new OAuth2Middleware($clientCredentials);

        $handlerStack = HandlerStack::create();
        $handlerStack->push($oauth2Middleware);

        return $handlerStack;
    }

   public function getFoo()
    {
        return json_decode(
            $this->client->get('/v1/foo')->getBody()->__toString(),
            true
        );
    }

No problem! The middleware system in Guzzle 6 is way different than Guzzle 5 and below. I actually had to go through the Guzzle source to figure out what exactly the create() function was supposed to take, if not a middleware.