laraveloauthlaravel-passportclientcredential

Passport - Create client credentials grant client programmatically


The documentation Here suggests php artisan passport:client --client for creating a client, but I want to do it using a controller, or ideally, using the native JSON apis provided by passport. Is that possible at all or will I have to override the methods in Passport::client ?


Solution

  • Old question, but here's a 2021 answer for people finding this on Google.

    I find calling Artisan commands from code unsavory, especially as @kishore-kadiyala mentioned because you get back printed output, rather than code.

    Instead, you can use the Laravel\Passport\ClientRepository class to create a client directly:

    use Laravel\Passport\ClientRepository;
    
    // ... snip ...
    
    $clients = App::make(ClientRepository::class);
    
    // 1st param is the user_id - none for client credentials
    // 2nd param is the client name
    // 3rd param is the redirect URI - none for client credentials
    $client = $clients->create(null, 'My Client Name', '');
    

    Here, $client is a Laravel\Passport\Client instance directly. This is exactly how the Artisan command creates the clients anyway.