Laravel Passport 不支持_grant_type

Posted

技术标签:

【中文标题】Laravel Passport 不支持_grant_type【英文标题】:Laravel Passport unsupported_grant_type 【发布时间】:2017-07-29 00:19:08 【问题描述】:

我已经被 Laravel Passport 问题困扰了 4 天了。 以下是我的代码库,用于验证登录凭据,从而验证用户登录(基于令牌)

我已按照 Passport 集成的所有步骤进行操作。

我调用的 API(来自 Postman):http://localhost/oauth/token/?email=admin@test.com&password=Abcd@123&grant_type=password&client_id=10&client_secret=xqVaBzFyC2fVDwyd6Pk7nsy2o45GfYAgUKeR2OjK&scope=*

email:admin@test.com
password:Abcd@123
grant_type:password
client_id:3
client_secret:xqVaBsdfsdfsf42fso45GfYAgUKeR2OjK
scope:*

配置/auth.php

'defaults' => [
        'guard' => 'api',
        'passwords' => 'users',
    ],

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
        ],
    ],

但我得到了错误:


  "error": "unsupported_grant_type",
  "message": "The authorization grant type is not supported by the authorization server.",
  "hint": "Check the `grant_type` parameter"

我收到错误

[2017-03-08 12:12:10] local.ERROR: League\OAuth2\Server\Exception\OAuthServerException: The authorization grant type is not supported by the authorization server. in /var/www/html/test/testProj/Proj_Latest/vendor/league/oauth2-server/src/Exception/OAuthServerException.php:65
Stack trace:
#0 /var/www/html/test/testProj/Proj_Latest/vendor/league/oauth2-server/src/AuthorizationServer.php(204): League\OAuth2\Server\Exception\OAuthServerException::unsupportedGrantType()
#1 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/passport/src/Http/Controllers/AccessTokenController.php(63): League\OAuth2\Server\AuthorizationServer->respondToAccessTokenRequest(Object(Zend\Diactoros\ServerRequest), Object(Zend\Diactoros\Response))
#2 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/passport/src/Http/Controllers/HandlesOAuthErrors.php(25): Laravel\Passport\Http\Controllers\AccessTokenController->Laravel\Passport\Http\Controllers\closure()
#3 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/passport/src/Http/Controllers/AccessTokenController.php(64): Laravel\Passport\Http\Controllers\AccessTokenController->withErrorHandling(Object(Closure))
#4 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(47): Laravel\Passport\Http\Controllers\AccessTokenController->issueToken(Object(Zend\Diactoros\ServerRequest))
#5 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Routing/Route.php(203): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(Laravel\Passport\Http\Controllers\AccessTokenController), 'issueToken')
#6 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Routing/Route.php(160): Illuminate\Routing\Route->runController()
#7 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Routing/Router.php(559): Illuminate\Routing\Route->run()
#8 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\Routing\Router->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#9 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(49): Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#10 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Routing\Middleware\ThrottleRequests->handle(Object(Illuminate\Http\Request), Object(Closure))
#11 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure(Object(Illuminate\Http\Request))
#12 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#13 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Routing/Router.php(561): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#14 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Routing/Router.php(520): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#15 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Routing/Router.php(498): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#16 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(174): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#17 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\closure(Object(Illuminate\Http\Request))
#18 /var/www/html/test/testProj/Proj_Latest/app/Http/Middleware/ResponseLogger.php(22): Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#19 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): App\Http\Middleware\ResponseLogger->handle(Object(Illuminate\Http\Request), Object(Closure))
#20 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure(Object(Illuminate\Http\Request))
#21 /var/www/html/test/testProj/Proj_Latest/app/Http/Middleware/RequestLogger.php(45): Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#22 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): App\Http\Middleware\RequestLogger->handle(Object(Illuminate\Http\Request), Object(Closure))
#23 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure(Object(Illuminate\Http\Request))
#24 /var/www/html/test/testProj/Proj_Latest/app/Http/Middleware/Cors.php(18): Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#25 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): App\Http\Middleware\Cors->handle(Object(Illuminate\Http\Request), Object(Closure))
#26 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure(Object(Illuminate\Http\Request))
#27 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#28 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#29 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure(Object(Illuminate\Http\Request))
#30 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(59): Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#31 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#32 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure(Object(Illuminate\Http\Request))
#33 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(30): Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#34 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#35 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure(Object(Illuminate\Http\Request))
#36 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(30): Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#37 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#38 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure(Object(Illuminate\Http\Request))
#39 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#40 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))
#41 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure(Object(Illuminate\Http\Request))
#42 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(46): Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#43 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#44 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure(Object(Illuminate\Http\Request))
#45 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#46 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(149): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#47 /var/www/html/test/testProj/Proj_Latest/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#48 /var/www/html/test/testProj/Proj_Latest/public/index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#49 main  

现在由于这不起作用,我尝试了该方法:

Auth::attempt(array('email' => 'admin@test.com', 'password' => 'Abcd@123'))

但这次我收到了:

[2017-03-08 12:00:00] local.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Call to undefined method Illuminate\Auth\RequestGuard::attempt() in /var/www/html /..../vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php:294

在 Laravel 5.4 中似乎没有任何工作

【问题讨论】:

Postman 中如何发送数据? form-data 或者 x-www-form-urlencoded ? 两种方式都试过了。还尝试了 URL 参数。所有结果为 unsupported_grant_type 您是否有从 Postman 在标头中发送的 Auth Basic 令牌或凭据?示例:Authorization: Basic aHR0cHdhdGNoOmY= No..由于这个API是检索token的,所以我没有在headers中设置任何token 【参考方案1】:

首先,确保授权客户端是“密码客户端”,创建方式与命令类似:

php artisan passport:client --password

这意味着数据库中的“password_client”列在对应客户端旁边的值为“1”。 对于从用户界面创建的默认用户客户端,我也遇到了很多困难,但它不适用于这些。

【讨论】:

以上是关于Laravel Passport 不支持_grant_type的主要内容,如果未能解决你的问题,请参考以下文章

_LIR__与Laravel 5.5和OVH共享主机不一致

Laravel 6 Passport CSRF 令牌不匹配

我应该还是不应该在 Laravel Passport 中使用带有密码授予授权的刷新令牌?

Laravel Passport 注册用户凭据不正确

Laravel Passport令牌过期方法不起作用

方法 Illuminate\Auth\RequestGuard::logout 不存在 Laravel Passport