[SOLVED] Slim4 + OAuth2 server issue

Hi there,
I’m trying to put together Slim4 and OAuth2 server (GitHub - bshaffer/oauth2-server-php: A library for implementing an OAuth2 Server in php). But IDE and Slim tell me that could not found \OAuth2\Server class, but it is in the lib folder and accessible.

But when I call my class with method which I need got this message:

Call to undefined function OAuth2\Server()

I think that something with my settings in Slim instance, but can’t understand what, because I’m newbie in Slim4 framework.

class OAuth2
{

    /** @var \OAuth2\Server */
    private $server;

    public function __construct(ContainerInterface $container)
    {
        $storage = new \OAuth2\Storage\Pdo([
            'dsn' => 'mysql:dbname=alice-grms;host=127.0.0.1',
            'username' => 'alice_grms',
            'password' => 'alice_grms',
        ]);

        $this->server = \OAuth2\Server($storage);

        // Add the "Client Credentials" grant type (it is the simplest of the grant types)
        $this->server->addGrantType(new ClientCredentials($storage));

        // Add the "Authorization Code" grant type (this is where the oauth magic happens)
        $this->server->addGrantType(new AuthorizationCode($storage));
    }

    public function token()
    {
        // Handle a request for an OAuth2.0 Access Token and send the response to the client
        $this->server->handleTokenRequest(\OAuth2\Request::createFromGlobals())->send();
    }
}

I don’t think you want it in your /lib folder … you should install it via composer. Try this:

composer require bshaffer/oauth2-server-php "^1.10"

This will place it in your /vendor folder and configure autoloader to use it.

Thanks for your reply, but I’m using composer and everything is up to date.

$ composer require bshaffer/oauth2-server-php
Using version ^1.11 for bshaffer/oauth2-server-php
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Writing lock file
Generating autoload files

OK, looking at your code, it seems you’re missing a “new” … try this:

$this->server = new \OAuth2\Server($storage);

Hopefully this is it.

OMG, my fault! Thanks for your attentiveness!