【问题标题】:Laravel Passport callback route returns nullLaravel Passport 回调路由返回 null
【发布时间】: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_clients where id = 3 limit 1) {"exception":"[object] (Illuminate\\Database\\QueryException(code: 42S02): SQLSTATE[42S02]: Base未找到表或视图:1146 表 'consumer.oauth_clients' 不存在(SQL:select * from oauth_clients where id = 3 limit 1)在 C:\\laragon\\www\\passport-app\ \vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php:671)
  • 您进行了迁移吗?

标签: php laravel guzzle laravel-passport


【解决方案1】:

所以我终于解决了问题,我只需要运行php artisan config:cache . [reference]

【讨论】:

  • 对我来说这行不通。我在EventServiceProvider 上遇到了一些passport 的事件,但出现了错误。
猜你喜欢
  • 2021-07-11
  • 1970-01-01
  • 2019-07-31
  • 2019-07-26
  • 1970-01-01
  • 2023-02-04
  • 1970-01-01
  • 2021-01-31
  • 2019-11-24
相关资源
最近更新 更多