2

我在我的项目中使用 Laravel Passport 身份验证。我只想在 API 中使用护照身份验证,所以我试图生成client_idclient_secret使用下面的代码,但它返回NULL

我已经在我的 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);
});

假设我client_id使用命令行生成php artisan passport:client

现在我想授权访问令牌,但它返回 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');将输出发送给我:

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

dd($response);将输出发送给我:

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: []
  }
}

谁能帮我吗?等待正面回应。

4

2 回答 2

0

我的团队领导已经解决了这个问题。请检查以下链接:

Laravel 的 5.3 护照和 api 路由

于 2016-11-03T10:06:50.440 回答
-1

您为 $response_body 获得 NULL 的原因是,当您将变量设置为类型提示 $response->getBody() 作为字符串时,您实际上得到的是 Stream 的一个实例。此外,您正在尝试 json_decode 一个类,而不是一个 json 字符串。$response_body = json_decode((string)$response->getBody(), true);如果您想要 JSON 响应,您可以这样做$response_body = $response->json();

有关更多信息,GuzzleHttp 文档的这些部分将非常有用: http ://guzzle3.readthedocs.io/http-client/response.html#response-body和http://guzzle3.readthedocs.io/http-client/实体body.html

于 2016-10-29T11:01:49.423 回答