如何在laravel中使用Guzzlehttp测试个人访问令牌

时间:2018-07-23 17:31:12

标签: php laravel oauth-2.0 laravel-passport guzzlehttp

我正在研究Laravel Passport。我创建了一个个人访问令牌。现在,我想使用guzzlehttp Client进行测试。我怎样才能做到这一点。 我正在尝试以这种方式无法正常工作。

routes/web.php

Route::get('/callback', function (Request $request) {
     $response = (new GuzzleHttp\Client)->post('http://passportauth.test/oauth/token', [
            'form_params' => [
                'grant_type' => 'authorization_code',
                'client_id' => 1, // Laravel Personal Access Client ID
                'client_secret' => 'zeFLAy76pZdCuHuKXjPCEgY0MMhnkADYbIRKzZQz', // Replace with client secret
                'redirect_uri' => 'http://passportauth.test/callback',
                'code' => $request->code,
            ]
        ]);

        session()->put('token', json_decode((string) $response->getBody(), true));

        return redirect('/test');
         //return json_decode((string) $response->getBody(), true);
    });

 Route::get('/test', function () {
   $response = (new GuzzleHttp\Client)->get('http://passportauth.test/api/test', [
       'headers' => [
           'Authorization' => 'Bearer '.session()->get('token.access_token')
       ]
   ]);

   return json_decode((string) $response->getBody(), true);
});

和我的routes/api.php

Route::get('/test',function(){
return 'This is an API Endpoints';
})->middleware('auth:api');

0 个答案:

没有答案