0
votes

I'm trying to create a bearer token for passport so I can use my api routes internally. I send a post request to /oauth/token with the client id and client secret. The code that generates the bearer token looks like this.

public function generateToken() {
    try {
        $req = Request::create('/oauth/token', 'POST', [
            'grant_type' => 'client_credentials',
            'client_id' => '1',
            'client_secret' => 'POe81tKvmZ0DhBdtwdM7VPoV5tTKobVYzSc9A0RX',
            'scope' => '*',
        ]);
        $res = app()->handle($req);
        return $res;
    } catch (Exception $e){
        log::critical('Failed to generate bearer token.');
    } catch (error $e) {
        log::critical('Failed to generate bearer token.');
    }

}

However when trying to do this request I get a 401 authorised.

  #content: "{"error":"invalid_client","message":"Client authentication failed"}" #version: "1.1"#statusCode: 401 #statusText: "Unauthorized"

Is there anyway of fixing this?

1
are you sure you're using the client_secret that corresponds to the client_id you're specifying?kburlz
I've managed to fix it. The way I was sending the parameters was incorrectJamie Woods

1 Answers

0
votes

This works for me

$client = new client(['verify' => false]);

$response = $client->post($url, [
            'form_params' => [
                'client_id' => 5,
                // The secret generated when you ran: php artisan passport:install
                'client_secret' => 'HxLB5LVi3HhHxYkoHVZinoYUz5Ab3HnbWmiB1KBd',
                'grant_type' => 'client_credentials',
                'scope' => '*',
            ]
        ]);