# \[SOLVED\] Slim4 + OAuth2 server issue

**URL:** <https://discourse.slimframework.com/t/solved-slim4-oauth2-server-issue/3446>\
**Category:** Questions\
**Created:** [September 17, 2019, 10:50pm UTC](https://discourse.slimframework.com/t/solved-slim4-oauth2-server-issue/3446 "2019-09-17T22:50:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Igelein](https://avatars.discourse-cdn.com/v4/letter/i/4bbf92/32.png) [@Igelein](https://discourse.slimframework.com/u/Igelein)\
**Post date:** [September 17, 2019, 10:50pm UTC](https://discourse.slimframework.com/t/solved-slim4-oauth2-server-issue/3446/1 "2019-09-17T22:50:42Z")

</div>

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](https://github.com/bshaffer/oauth2-server-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.

```auto
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();
    }
}

```

---

<div class="post-metadata">

**Author:** ![TheLobos](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@TheLobos](https://discourse.slimframework.com/u/TheLobos)\
**Post date:** [September 18, 2019, 5:51am UTC](https://discourse.slimframework.com/t/solved-slim4-oauth2-server-issue/3446/2 "2019-09-18T05:51:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Igelein](https://avatars.discourse-cdn.com/v4/letter/i/4bbf92/32.png) [@Igelein](https://discourse.slimframework.com/u/Igelein)\
**Post date:** [September 18, 2019, 9:13am UTC](https://discourse.slimframework.com/t/solved-slim4-oauth2-server-issue/3446/3 "2019-09-18T09:13:09Z")

</div>

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

```auto
$ 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

```

---

<div class="post-metadata">

**Author:** ![TheLobos](https://avatars.discourse-cdn.com/v4/letter/t/e9bcb4/32.png) [@TheLobos](https://discourse.slimframework.com/u/TheLobos)\
**Post date:** [September 18, 2019, 10:07am UTC](https://discourse.slimframework.com/t/solved-slim4-oauth2-server-issue/3446/4 "2019-09-18T10:07:47Z")

</div>

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

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

```

Hopefully this is it.

---

<div class="post-metadata">

**Author:** ![Igelein](https://avatars.discourse-cdn.com/v4/letter/i/4bbf92/32.png) [@Igelein](https://discourse.slimframework.com/u/Igelein)\
**Post date:** [September 18, 2019, 12:03pm UTC](https://discourse.slimframework.com/t/solved-slim4-oauth2-server-issue/3446/5 "2019-09-18T12:03:35Z")

</div>

OMG, my fault! Thanks for your attentiveness!
