【问题标题】:Laravel passport oauth/authorize errorLaravel 护照 oauth/授权错误
【发布时间】:2026-01-21 21:00:02
【问题描述】:

我正在尝试通过 oauth/authorize 使用 Passport,以允许 Web 应用程序稍后获取代码和请求令牌,但出现错误

路由 [登录名] 未定义

下面是我的代码。

客户端代码

    // First route that user visits on consumer app
Route::get('/', function () {
    // Build the query parameter string to pass auth information to our request
    $query = http_build_query([
        'client_id' => 3,
        //'client_secret' => 'MtkEmBL0f0Bf4LcEPcOBUS0wLHvF5xqqchhCpaTH',
        'redirect_uri' => 'http://client.app:8082/callback',
        'response_type' => 'code',
        'scope' => ''
    ]);

    // Redirect the user to the OAuth authorization page
    return redirect('http://server.app:8082/oauth/authorize?' . $query);
});


// Route that user is forwarded back to after approving on server
Route::get('/callback', function (Request $request) {
    return 'test 2';
    $http = new GuzzleHttp\Client;

    $response = $http->post('http://server.app:8082/oauth/token', [
        'form_params' => [
            'grant_type' => 'authorization_code',
            'client_id' => 3, // from admin panel above
            'client_secret' => 'MtkEmBL0f0Bf4LcEPcOBUS0wLHvF5xqqchhCpaTH', // from admin panel above
            'redirect_uri' => 'http://client.app:8082/callback',
            'code' => $request->code // Get code from the callback
        ]
    ]);

    // echo the access token; normally we would save this in the DB
    return json_decode((string) $response->getBody(), true)['access_token'];
});

【问题讨论】:

  • 你在哪里定义认证路由?
  • 嗨舒克申!我的路线在 AuthServiceProvider 文件中定义,如下所示.. Passport::routes();
  • 问题,你在哪里定义了/oauth/authorize 路由?我将 oneAuth 客户端用于更复杂的场景,但这部分流程也适用于我的案例

标签: php laravel oauth-2.0 laravel-passport


【解决方案1】:

也许你有不止一个错误。看起来您忘记定义常见的身份验证路由。从php artisan make:authAuth::routes() 开始。 OAuth 路由没有login 路由,你得到的错误说你没有定义login 路由。它实际上是在Auth::routes() 中定义的。

【讨论】:

  • 但是 php artisan make:auth 会为用户登录、注册等创建脚手架模板,但它是一个 api,所以它不应该有 html 页面,而是应该在命令用户授予应用程序权限,以便在获得用户权限后请求令牌...
  • 如果您不需要它的视图\布局,只需将Auth::routes() 添加到您的路由文件中。
  • 等一下,如果您安装了 Passport,则表示该用户可以登录该网站。登录后,用户接受来自其他服务的 oauth 请求。否则你认为他如何接受 oauth 请求?
  • 好吧,如果我错了,请纠正我,但是,客户端(我的 Web 应用程序)会将 client_id 和秘密发送到 API(Oauth),然后 API 将返回权限页面,用户可以拒绝或接受 Web App 来请求令牌,如果是这样,用户将键入电子邮件和密码,这将通过 post 请求连同 client_secret、id 和 grant_type 密码一起发送,然后API 将返回访问令牌。对吗?非常感谢!
  • 我不确定当用户未在 OAuth 提供程序服务器上获得授权时它是如何工作的。无论如何,您的 OAuth 服务器拥有所有用户、所有身份验证逻辑和登录表单。这就是为什么必须拥有身份验证路由的原因。用户必须登录才能接受\拒绝授权请求。
【解决方案2】:

我有同样的问题,显然,我没有在请求中传递 Accept 标头

Accept:application/json

【讨论】:

    最近更新 更多