Search code examples
phplaravellaravel-passport

Consuming API with Laravel Passport


I dont understand how to send the token:

$response = $client->request('GET', '/api/user', [
'headers' => [
    'Accept' => 'application/json',
    'Authorization' => 'Bearer '.$accessToken,
],

]);

If I have a HTML Form how can I send the token to consume the API?

Thanks


Solution

  • You need to authenticate in the API first to get the token and then send any other request using that token.

    Examples with Curl:

    Authentication first:

        $curl = curl_init();
    
        curl_setopt_array($curl, array(
            CURLOPT_URL => "http://laravel-app-with-passport.com/oauth/token",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => "grant_type=password&client_id=".$outhCliendIdGeneratedInLaravelPassport ."&client_secret=".$OuthClientSecretGenerateInLaravelPassport ."&username=". $validLaravelUserName . "&password=" . $validLaravelUserPassword,
            CURLOPT_HTTPHEADER => array(
                "accept: application/json",
                "content-type: application/x-www-form-urlencoded",
            ),
        ));
    
        $response = curl_exec($curl);
        $err = curl_error($curl);
    
        $responseInfo = curl_getinfo($curl);
    
        curl_close($curl);
    
        if ($err) {
            set_error_handler($err);
        } else {
    
            if ($responseInfo['http_code'] == 200) {
    
                $_SESSION['TOKEN'] = json_decode($response)->access_token;
                $_SESSION['TOKE_TYPE'] = json_decode($response)->token_type;
    
            } else {
    
                set_error_handler("Login to api faild status 403");
                error_log("No login api status 403");
    
            }
        }
    

    Then, you can send the the token and consume any url in the api:

        $curl = curl_init();
    
        curl_setopt_array($curl, array(
            CURLOPT_URL => "http://laravel-app-with-passport.com/api/someurl",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => $method,
            CURLOPT_POSTFIELDS => null, // here you can send parameters
            CURLOPT_HTTPHEADER => array(
                "accept: application/json",
                "authorization: " . $_SESSION['TOKE_TYPE'] . " " . $_SESSION['TOKEN'] . "",
                "cache-control: no-cache",
                "content-type: application/json",
            ),
        ));
    
        $response = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
    
        var_dump($response);
    

    Also, as someone said before, you can use instead of curl, Guzzle HTTP Client (https://github.com/guzzle/guzzle)