Laravel 护照 500 内部服务器错误

Posted

技术标签:

【中文标题】Laravel 护照 500 内部服务器错误【英文标题】:Laravel passport 500 Internal Server Error 【发布时间】:2019-02-10 19:01:49 【问题描述】:

所以我将 laravel 与护照一起使用,但我遇到了问题。当我向我的 AuthController@login 发出发布请求时,它给了我一个 500 内部服务器错误。这是我正在使用的东西

我将 laragon 与 apache 服务器和 mysql 一起用于环境。我可以毫无问题地向我的注册路线提出请求,但我的登录路线一直失败。不知道我错过了什么

这是我的 AuthController.php

public function login(Request $request)

    $http = new \GuzzleHttp\Client;

    try 
        $response = $http->post(config('services.passport.login_endpoint'), [
            'form_params' => [
                'grant_type' => 'password',
                'client_id' => config('services.passport.client_id'),
                'client_secret' => config('services.passport.client_secret'),
                'username' => $request->username,
                'password' => $request->password,
            ]
        ]);
        return $response->getBody();
     catch (\GuzzleHttp\Exception\BadResponseException $e) 
        if ($e->getCode() === 400) 
            return response()->json('Invalid Request. Please enter a username or a password.', $e->getCode());
         else if ($e->getCode() === 401) 
            return response()->json('Your credentials are incorrect. Please try again', $e->getCode());
        
        return response()->json('Something went wrong on the server.', $e->getCode());
    

这是我存储在 .env 文件中的变量

PASSPORT_LOGIN_ENDPOINT="http://traxit-backend2.test/oauth/token"
PASSPORT_CLIENT_ID=2
PASSPORT_CLIENT_SECRET=3xlUZVF1fP5fHOQgXAY2Vd7xrIsGjGNRagcMcfU5

这是我的 api 路由

Route::middleware('auth:api')->get('/user', function (Request $request) 
    return $request->user();
);

Route::post('/login', 'AuthController@login');
Route::post('/register', 'AuthController@register');
Route::middleware('auth:api')->post('/logout', 'AuthController@logout');

这是我得到的日志

    GuzzleHttp\Exception\RequestException->() C:\laragon\www\traxit-backend2\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php:186 GuzzleHttp\Handler\CurlFactory->createRejection() C:\laragon\www\traxit-backend2\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php:149 GuzzleHttp\Handler\CurlFactory->finishError() C:\laragon\www\traxit-backend2\vendor\guzzlehttp\guzzle\src\Handler\CurlFactory.php:102 GuzzleHttp\Handler\CurlFactory->finish() C:\laragon\www\traxit-backend2\vendor\guzzlehttp\guzzle\src\Handler\CurlHandler.php:43 GuzzleHttp\Handler\CurlHandler->__invoke() C:\laragon\www\traxit-backend2\vendor\guzzlehttp\guzzle\src\Handler\Proxy.php:28 GuzzleHttp\Handler\Proxy->GuzzleHttp\Handlerclosure() C:\laragon\www\traxit-backend2\vendor\guzzlehttp\guzzle\src\Handler\Proxy.php:51 GuzzleHttp\Handler\Proxy->GuzzleHttp\Handlerclosure() C:\laragon\www\traxit-backend2\vendor\guzzlehttp\guzzle\src\PrepareBodyMiddleware.php:66 GuzzleHttp\PrepareBodyMiddleware->__invoke() C:\laragon\www\traxit-backend2\vendor\guzzlehttp\guzzle\src\Middleware.php:30 GuzzleHttp\Middleware->GuzzleHttpclosure() C:\laragon\www\traxit-backend2\vendor\guzzlehttp\guzzle\src\RedirectMiddleware.php:70 GuzzleHttp\RedirectMiddleware->__invoke() C:\laragon\www\traxit-backend2\vendor\guzzlehttp\guzzle\src\Middleware.php:60 GuzzleHttp\Middleware->GuzzleHttpclosure() C:\laragon\www\traxit-backend2\vendor\guzzlehttp\guzzle\src\HandlerStack.php:67 GuzzleHttp\HandlerStack->__invoke() C:\laragon\www\traxit-backend2\vendor\guzzlehttp\guzzle\src\Client.php:277 GuzzleHttp\Client->transfer() C:\laragon\www\traxit-backend2\vendor\guzzlehttp\guzzle\src\Client.php:125 GuzzleHttp\Client->requestAsync() C:\laragon\www\traxit-backend2\vendor\guzzlehttp\guzzle\src\Client.php:131 GuzzleHttp\Client->request() C:\laragon\www\traxit-backend2\vendor\guzzlehttp\guzzle\src\Client.php:89 GuzzleHttp\Client->__call() C:\laragon\www\traxit-backend2\app\Http\Controllers\AuthController.php:22 App\Http\Controllers\AuthController->login() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Routing\Controller.php:54 call_user_func_array() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Routing\Controller.php:54 Illuminate\Routing\Controller->callAction() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php:45 Illuminate\Routing\ControllerDispatcher->dispatch() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Routing\Route.php:212 Illuminate\Routing\Route->runController() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Routing\Route.php:169 Illuminate\Routing\Route->run() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Routing\Router.php:679 Illuminate\Routing\Router->Illuminate\Routingclosure() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:30 Illuminate\Routing\Pipeline->Illuminate\Routingclosure() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Routing\Middleware\SubstituteBindings.php:41李> Illuminate\Routing\Middleware\SubstituteBindings->handle() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:151 Illuminate\Pipeline\Pipeline->Illuminate\Pipelineclosure() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53 Illuminate\Routing\Pipeline->Illuminate\Routingclosure() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Routing\Middleware\ThrottleRequests.php:57李> Illuminate\Routing\Middleware\ThrottleRequests->handle() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:151 Illuminate\Pipeline\Pipeline->Illuminate\Pipelineclosure() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53 Illuminate\Routing\Pipeline->Illuminate\Routingclosure() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:104 Illuminate\Pipeline\Pipeline->then() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Routing\Router.php:681 Illuminate\Routing\Router->runRouteWithinStack() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Routing\Router.php:656 Illuminate\Routing\Router->runRoute() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Routing\Router.php:622 Illuminate\Routing\Router->dispatchToRoute() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Routing\Router.php:611 Illuminate\Routing\Router->dispatch() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:176 Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Httpclosure() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:30 Illuminate\Routing\Pipeline->Illuminate\Routingclosure() C:\laragon\www\traxit-backend2\vendor\barryvdh\laravel-cors\src\HandleCors.php:36 Barryvdh\Cors\HandleCors->handle() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:151 Illuminate\Pipeline\Pipeline->Illuminate\Pipelineclosure() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53 Illuminate\Routing\Pipeline->Illuminate\Routingclosure() C:\laragon\www\traxit-backend2\vendor\fideloper\proxy\src\TrustProxies.php:57 Fideloper\Proxy\TrustProxies->handle() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:151 Illuminate\Pipeline\Pipeline->Illuminate\Pipelineclosure() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53 Illuminate\Routing\Pipeline->Illuminate\Routingclosure() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php:31 Illuminate\Foundation\Http\Middleware\TransformsRequest->handle() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:151 Illuminate\Pipeline\Pipeline->Illuminate\Pipelineclosure() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53 Illuminate\Routing\Pipeline->Illuminate\Routingclosure() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php:31 Illuminate\Foundation\Http\Middleware\TransformsRequest->handle() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:151 Illuminate\Pipeline\Pipeline->Illuminate\Pipelineclosure() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53 Illuminate\Routing\Pipeline->Illuminate\Routingclosure() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php:27 Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:151 Illuminate\Pipeline\Pipeline->Illuminate\Pipelineclosure() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53 Illuminate\Routing\Pipeline->Illuminate\Routingclosure() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php:62 Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:151 Illuminate\Pipeline\Pipeline->Illuminate\Pipelineclosure() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53 Illuminate\Routing\Pipeline->Illuminate\Routingclosure() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:104 Illuminate\Pipeline\Pipeline->then() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:151 Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter() C:\laragon\www\traxit-backend2\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php:116 Illuminate\Foundation\Http\Kernel->handle() C:\laragon\www\traxit-backend2\public\index.php:55

【问题讨论】:

您找到解决方案了吗?有同样的问题 @TrueCode,请参阅下面的答案。我必须将passport 变量添加到我的services.php 配置文件中。 【参考方案1】:

问题在于我的services 配置文件中的services.passport 不存在。我把它放在我的services.php 配置文件中。

'passport' => [
        'login_endpoint'  => env('TOKEN_LOGIN_ENDPOINT'),
        'client_id'    => env('TOKEN_CLIENT_ID'),
        'client_secret' => env('TOKEN_CLIENT_SECRET'),
    ],

【讨论】:

以上是关于Laravel 护照 500 内部服务器错误的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 5.7 ajax 请求 500(内部服务器错误)

GET 500(内部服务器错误)laravel/js

内部服务器错误 500 Laravel 5.4 AJAX

Laravel 5:Ajax Post 500(内部服务器错误)错误

Laravel 得到 500 内部服务器错误

laravel 中的 Ajax 发布返回错误 500(内部服务器错误)