2
votes

I am trying to develop a restful api using token for authentication using laravel 5.3 . I have also added laravel passport and I thought it will help me build the api faster. The resource I would like to give through api is common to all users , nothing specific to the user. So figured I should use grant type : client credentials. I couldn't find the any help about this grant type in laravel documentation

https://github.com/laravel/passport/pull/34/files this link shows the client credential grant type is added to laravel passport . but when I try to give grant type as client credential

    $http = new \GuzzleHttp\Client;
    $response = $http->post('http://site.dev/oauth/token', [
        'form_params' => [
            'grant_type' => 'client_credentials',
            'client_id' => config('api.client_id'),
            'client_secret' => config('api.client_secret'),
            'redirect_uri' => config('api.redirect_uri'),
     ],
    ]);

it gives error in grant type used.

2
I think i should go with password grant type.cody 007
I have created a client using password grant and edited the database table column password_client , changed 1 -> 0. and it return api token. how to create password client ? there is only option using php artsan commandcody 007

2 Answers

2
votes

I was using client credentials method because it was a machine to machine call. I got error like { "error": "Unauthenticated." }` when using postman. I used Access type and Authorization headers but was getting the same error. Then I googled and tried the method explained in below link. It worked. :) :) http://filljoyner.com/2017/03/01/how-to-use-client-credentials-grant-tokens-for-your-api-authorization-with-laravel-5-4s-passport/

1
votes

When you install Laravel Passport one password grant in installed by default in oath_clients table, but password_client column should be 1 not 0.
Then you send some form_data to /oauth/token url and get access_token in return.