2

I am using Laravel Passport Authentication in my project. I want to use passport authentication only in API so I am trying to generate client_id and client_secret using below code but it returns NULL

I have written this code in my routes/api.php

Route::post('/gen_client', function () {
    $http = new GuzzleHttp\Client([
        'headers' => [ 'Content-Type' => 'application/json' ]
    ]);

    $response = $http->post(url('/') . '/oauth/clients',
    ['body' => json_encode(
        [
            'id' => 'ovais.khan@gmail.com', 
            'name' => 'Ovais2',
            'redirect' => url('/') . '/callback'
        ]
        )
    ]
);

    $response_body = json_decode((string)$response->getBody(), true); 
    var_dump($response_body);
});

Suppose if I have generated client_id using command line php artisan passport:client

now I want to authorized access token but it returns NULL

Route::post('callback', function (Request $request) {
    $http = new GuzzleHttp\Client();  
    $oauth_client = DB::table('oauth_clients')->where('id', '=', 'ovais.khan@gmail.com')->first();   
    $response = $http->post(url('/') . '/oauth/token', [        
        'form_params' => [
            'grant_type' => 'authorization_code',
            'client_id' => $oauth_client->id,
            'client_secret' => $oauth_client->secret,
            'redirect_uri' => url('/') . '/callback',
            'code' => $request->code,
        ],
    ]);  
    $response_body = json_decode((string)$response->getBody(), true);  
    var_dump($response_body);
    $access_token = $response_body['access_token'] ;  
    $refresh_token = $response_body['refresh_token'];  
});

dd(url('/') . '/oauth/clients'); send me the output:

http://localhost/project/public/oauth/clients

dd($response); send me the output:

Response {#224
  -reasonPhrase: "OK"
  -statusCode: 200
  -headers: array:7 [
    "Date" => array:1 [
      0 => "Sat, 29 Oct 2016 10:19:24 GMT"
    ]
    "Server" => array:1 [
      0 => "Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/7.0.9"
    ]
    "X-Powered-By" => array:1 [
      0 => "PHP/7.0.9"
    ]
    "Cache-Control" => array:1 [
      0 => "no-cache"
    ]
    "Set-Cookie" => array:1 [
      0 => "laravel_session=eyJpdiI6Ikx5WXJpTEFvZ0ZiNFwvbFwvYWZRQWxEZz09IiwidmFsdWUiOiJVM29lbTNYSnZwdld5ZHdVRk1IK1hJbG9RenNQa1owS1lRMEFYQ3lVamluV3JcL2RwaVQyRkpsZzkwQ082OU94QkJiSUpGQTZpeTMxWjdpMEtCZ1Byc3c9PSIsIm1hYyI6IjQwNTcwZTQ4YzQ2OGIwODQ5Y2NjMzBiMmIyNmI5MTVkNTY0ZjI0OGQ1Y2M1NTVjYTljNmU4Mjk2Nzg0Yjk2MmMifQ%3D%3D; expires=Sat, 29-Oct-2016 12:19:24 GMT; Max-Age=7200; path=/; HttpOnly"
    ]
    "Content-Length" => array:1 [
      0 => "4709"
    ]
    "Content-Type" => array:1 [
      0 => "text/html; charset=UTF-8"
    ]
  ]
  -headerNames: array:7 [
    "date" => "Date"
    "server" => "Server"
    "x-powered-by" => "X-Powered-By"
    "cache-control" => "Cache-Control"
    "set-cookie" => "Set-Cookie"
    "content-length" => "Content-Length"
    "content-type" => "Content-Type"
  ]
  -protocol: "1.1"
  -stream: Stream {#222
    -stream: stream resource @234
      wrapper_type: "PHP"
      stream_type: "TEMP"
      mode: "w+b"
      unread_bytes: 0
      seekable: true
      uri: "php://temp"
      options: []
    }
    -size: null
    -seekable: true
    -readable: true
    -writable: true
    -uri: "php://temp"
    -customMetadata: []
  }
}

Can anyone help me out? Waiting for positive response.

okconfused
  • 3,567
  • 4
  • 25
  • 38
  • Have you tried to dd(url('/') . '/oauth/clients'), to make sure this is evaluating to what I presume you want of `domain.com/oauth/clients`? Have you tried to `dd($response);` and `dd($response->getBody());`? If not please try these and let us know what the output is. – alex-greaves Oct 29 '16 at 10:06
  • `dd(url('/') . '/oauth/clients');` send me the output: `http://localhost/project/public/oauth/clients` – okconfused Oct 29 '16 at 10:15
  • Is that the correct URL you want to post to? Also have you tried to `dd($response);` and `dd($response->getBody());`? If so please send the output for these. – alex-greaves Oct 29 '16 at 10:23
  • The reponse is very big so i am sending you into parts dd($response); send me the output: 'Response {#224 -reasonPhrase: "OK" -statusCode: 200 -headers: array:7 [ "Date" => array:1 [ 0 => "Sat, 29 Oct 2016 10:19:24 GMT" ] "Server" => array:1 [ 0 => "Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/7.0.9" ] "X-Powered-By" => array:1 [ 0 => "PHP/7.0.9" ] "Cache-Control" => array:1 [ 0 => "no-cache" ]' – okconfused Oct 29 '16 at 10:24
  • '"Set-Cookie" => array:1 [ 0 => "laravel_session=eyJpdiI6Ikx5WXJpTEFvZ0ZiNFwvbFwvYWZRQWxEZz09IiwidmFsdWUiOiJVM29lbTNYSnZwdld5ZHdVRk1IK1hJbG9RenNQa1owS1lRMEFYQ3lVamluV3JcL2RwaVQyRkpsZzkwQ082OU94QkJiSUpGQTZpeTMxWjdpMEtCZ1Byc3c9PSIsIm1hYyI6IjQwNTcwZTQ4YzQ2OGIwODQ5Y2NjMzBiMmIyNmI5MTVkNTY0ZjI0OGQ1Y2M1NTVjYTljNmU4Mjk2Nzg0Yjk2MmMifQ%3D%3D; expires=Sat, 29-Oct-2016 12:19:24 GMT; Max-Age=7200; path=/; HttpOnly" ] "Content-Length" => array:1 [ 0 => "4709" ] "Content-Type" => array:1 [ 0 => "text/html; charset=UTF-8" ] ]' – okconfused Oct 29 '16 at 10:25
  • ` -headerNames: array:7 [ "date" => "Date" "server" => "Server" "x-powered-by" => "X-Powered-By" "cache-control" => "Cache-Control" "set-cookie" => "Set-Cookie" "content-length" => "Content-Length" "content-type" => "Content-Type" ]` – okconfused Oct 29 '16 at 10:25
  • `-protocol: "1.1" -stream: Stream {#222 -stream: stream resource @234 wrapper_type: "PHP" stream_type: "TEMP" mode: "w+b" unread_bytes: 0 seekable: true uri: "php://temp" options: [] } -size: null -seekable: true -readable: true -writable: true -uri: "php://temp" -customMetadata: [] } }` – okconfused Oct 29 '16 at 10:25
  • this is the total output – okconfused Oct 29 '16 at 10:25
  • I have edit the post with output – okconfused Oct 29 '16 at 10:29
  • Thank you, can you also please send across the results of `dd($response->getBody());`? Need to check that access_token is actually part of the array. Also in your question you mentioned it returns NULL, is it $response_body which is returning NULL or $response_body['access_token']? – alex-greaves Oct 29 '16 at 10:45
  • `Stream {#222 -stream: stream resource @234 wrapper_type: "PHP" stream_type: "TEMP" mode: "w+b" unread_bytes: 0 seekable: true uri: "php://temp" options: [] } -size: null -seekable: true -readable: true -writable: true -uri: "php://temp" -customMetadata: [] }` – okconfused Oct 29 '16 at 10:51
  • above is the response of `dd($response->getBody());` and $response_body sending me the NULL – okconfused Oct 29 '16 at 10:52

2 Answers2

0

My teamlead has been resolved this problem. Please check the below link:

Laravel's 5.3 passport and api routes

Community
  • 1
  • 1
okconfused
  • 3,567
  • 4
  • 25
  • 38
-1

The reason you are getting NULL for $response_body is when you are setting the variable you are type hinting $response->getBody() as a string, what you are actually getting is an instance of Stream. Also you're trying to json_decode a class, not a json string. $response_body = json_decode((string)$response->getBody(), true); If you would like a JSON response you can do $response_body = $response->json();

For more information these sections of the GuzzleHttp docs will be really useful: http://guzzle3.readthedocs.io/http-client/response.html#response-body and http://guzzle3.readthedocs.io/http-client/entity-bodies.html

alex-greaves
  • 189
  • 7
  • I am getting this error Call to undefined method GuzzleHttp\Psr7\Response::json() – okconfused Oct 29 '16 at 11:21
  • Sorry, I presumed you'd be using Guzzle 5.3 which includes that method in its [response](https://github.com/guzzle/guzzle/blob/5.3/src/Message/Response.php#L138), is there a particular reason you aren't using Guzzle 5.3? Perhaps you can just do `dd(json_decode($response->getBody()));` to check that it doesn't return NULL, if this does it'd be worth checking your endpoint to make sure you're returning something. – alex-greaves Oct 29 '16 at 11:37
  • `dd(json_decode($response->getBody()));` return `NULL` – okconfused Oct 29 '16 at 11:54