knpuniversity / oauth2-client-bundle

Easily talk to an OAuth2 server for social functionality in Symfony
https://symfonycasts.com
MIT License
776 stars 146 forks source link

Symfony3.3 Error "requires that you provide a value for the "$clientRegistry" argument" #142

Closed iamromeo closed 3 years ago

iamromeo commented 5 years ago

Note:

  1. During installation, I had to do some simplification here since this is built with Symfony4 in mind. so my configuration is on the config.yml file
  2. Also for those not using flex The line to add to AppKernel is: new KnpU\OAuth2ClientBundle\KnpUOAuth2ClientBundle(),

Issue:

I am using Symfon 3.x and PHP7

I have followed the instructions and I ended up with the following configuration and code:

knpu_oauth2_client:
    clients:
        discord_client:
            type: discord
            client_id: 'ID'
            client_secret: 'SECRET'
            redirect_route: discord_connect_check
            redirect_params: {}
<?php
namespace UserBundle\Controller;

use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class DiscordController extends Controller
{

    public function connectAction(ClientRegistry $clientRegistry)
    {
        return $clientRegistry
            ->getClient('discord_client') 
            ->redirect([
                'identify', 'email'
            ])
        ;
    }

    public function connectCheckAction(Request $request, ClientRegistry $clientRegistry)
    {
        $client = $clientRegistry->getClient('discord_client');
        try {
            $user = $client->fetchUser();
            $accessToken = $client->getAccessToken();
            $userFromToken = $client->fetchUserFromToken($accessToken);
            var_dump($user); 
            var_dump($accessToken);
            var_dump($userFromToken); 
            die;
        } catch (IdentityProviderException $e) {
            var_dump($e->getMessage()); die;
        }
    }
}

But every time I navigate to /connect/discord/check (first method above) I get this:

Controller "UserBundle\Controller\DiscordController::connectAction()" requires that you provide a value for the "$clientRegistry" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.

Can someone give me a pointer here because I am so very lost.

sadikoff commented 5 years ago

@iamromeo Hi, everything looks good, you did a great job to run this code with sf 3.3. As I see there is one little moment left. Symfony 3.3 doesn't use autowiring with controller actions that's why you cannot use

public function connectAction(ClientRegistry $clientRegistry)

You should get $clientRegistry directly through the container like

public function connectAction()
    {
        return $this->get('knpu.oauth2.registry') // or 'oauth2.registry'
            ->getClient('discord_client') 
            ->redirect([
                'identify', 'email'
            ])
        ;
    }

Hope this will help you! Cheers!

jakobeissler commented 5 years ago

Was looking for this. It´s working. Thanks.