方法 Laravel\\Passport\\Bridge\\AccessToken::__toString() 不得抛出异常

Method Laravel\\Passport\\Bridge\\AccessToken::__toString() must not throw an exception

我使用这些版本的包来为用户登录创建令牌,但是我在创建令牌时遇到了这个错误:

composer.json

"require": {
    "php": "^7.3|^8.0",
    "fideloper/proxy": "^4.4",
    "fruitcake/laravel-cors": "^2.0",
    "guzzlehttp/guzzle": "^7.0.1",
    "laravel/framework": "^8.12",
    "laravel/passport": "^10.1.3",
    "laravel/tinker": "^2.5",
    "laravel/ui": "^3.2",
    "laravelcollective/html": "^6.2",
    "lcobucci/jwt": "3.4.5",
    "spatie/laravel-permission": "^4.0"
},
"require-dev": {
    "barryvdh/laravel-ide-helper": "^2.9",
    "facade/ignition": "^2.5",
    "fakerphp/faker": "^1.9.1",
    "laravel/sail": "^1.0.1",
    "mockery/mockery": "^1.4.2",
    "nunomaduro/collision": "^5.0",
    "phpunit/phpunit": "^9.3.3"
}

routes/api.php

Route::post('login', [ApiLoginController::class, 'login']);

ApiLoginController.php

public function login(Request $request)
{
    $this->validateLogin($request);

    $user = User::where($this->username, $request->get('username'))
        ->first();

    if (Auth::attempt([$this->username => $request->get('username'), 'password' => $request->get('password')])) {

        if ($user->status != 'active') {
            $msg = 'Account is not active';

            return $this->customError($msg);
        }

        $success['token'] = $user->createToken('Personal Access Client')->accessToken;
        $success['name'] = $user->name;
        $success['phone'] = $user->phone;
        $success['email'] = $user->email;

        $user->password = null;
        $user->save();

        return $this->success($success, "Login completed successfully");

    }

    //delete user password
    if ($user){
        $user->password = null;
        $user->save();
    }

    $msg = 'The information entered does not match our information';

    return $this->customError($msg);
}

邮递员回复

{
"message": "Method Laravel\Passport\Bridge\AccessToken::__toString() must not throw an exception, caught Lcobucci\JWT\Signer\InvalidKeyProvided: It was not possible to parse your key, reason: error:0908F070:PEM routines:get_header_and_data:short header",
"exception": "Symfony\Component\ErrorHandler\Error\FatalError",
"file": "F:\xampp\htdocs\Diapad-BackEnd\vendor\league\oauth2-server\src\ResponseTypes\BearerTokenResponse.php",
"line": 0,
"trace": []
}

此过程无需令牌行即可运行。甚至创建了一个令牌,但没有作为字符串返回。 我多次降低甚至升级我的包版本,但都没有用。

您可能使用的是 PHP 7.4 之前的版本。

__toString() 方法中抛出异常被 this RFC 允许,PHP 7.4

接受了该异常

laravel/passportrelies on lcobucci/jwt as well as the required league/oauth2-server also does

lcobucci/jwt 有一个 minimum of PHP 7.4 dependency written,你的 composer installcomposer update 应该在某个时候捕捉到,除非你没有自己安装或使用 composer 带有 --ignore-platform-reqs 标志。