【发布时间】:2020-12-17 13:41:31
【问题描述】:
我正在学习 Laravel Passport 并开发 OAuth2 服务器。创建客户端后,我进行了以下调用以获取授权码
Route::get('/', function (Request $request) {
$request->session()->put('state', $state = Str::random(40));
$query = http_build_query([
'client_id' => <Client_Id>,
'redirect_uri' => 'http://<Consumer_App_URL>/callback',
'response_type' => 'code',
'scope' => '',
'state' => $state,
]);
return redirect('http://<OAuth2_Server_URL>/oauth/authorize?'.$query);
});
调用按预期工作,我得到了授权码,但是当重定向到这样定义的回调路由时
Route::get('/callback', function (Request $request) {
$state = $request->session()->pull('state');
throw_unless(
strlen($state) > 0 && $state === $request->state,
InvalidArgumentException::class
);
$response = Http::asForm()->post('http://<OAuth2_Server_URL>/oauth/token', [
'grant_type' => 'authorization_code',
'client_id' => <Client_Id>,
'client_secret' => '<Cient_Secret>',
'redirect_uri' => 'http://consumer/callback',
'code' => $request->code,
]);
return $response->json();
});
它不起作用,响应实例只是空。我不知道可能是什么问题。
【问题讨论】:
-
尝试在 laravel/apache 日志文件中查找有关错误的更多信息
-
因此,当检查来自浏览器的请求时,它显示状态为 200,但在调用 $request->status() 时,它显示错误 500
-
好的,再次检查 laravel 日志后,它似乎尝试连接到我的“消费者应用程序”数据库并从“oauth_clients”表中选择,尽管我的消费者中没有这样的表。我不明白为什么它会这样
-
这里是确切的日志记录 [2020-12-14 14:50:01] local.ERROR: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'consumer.oauth_clients' 没有'不存在 (SQL: select * from
oauth_clientswhereid= 3 limit 1) {"exception":"[object] (Illuminate\\Database\\QueryException(code: 42S02): SQLSTATE[42S02]: Base未找到表或视图:1146 表 'consumer.oauth_clients' 不存在(SQL:select * fromoauth_clientswhereid= 3 limit 1)在 C:\\laragon\\www\\passport-app\ \vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php:671) -
您进行了迁移吗?
标签: php laravel guzzle laravel-passport