laravel10 使用passport 密码授权令牌无法使用,报错unsupported_grant_type

1. 运行环境

本地运行windows
php版本8.1.23
laravel10.*

1). 当前使用的 Laravel 版本?

laravel10 使用passport 密码授权令牌无法使用unsupported_grant_type

3). 当前系统

windows10

2. 问题描述?

使用passport 密码授权的时候无法获取token
报错

laravel10 使用passport 密码授权令牌无法使用unsupported_grant_type

我的安装步骤如下:

composer create-project --prefer-dist laravel/laravel=10.* laravel

.env 数据库配置
修改数据库默认字符串长度
   Schema::defaultStringLength(191);
composer require laravel/passport
Laravel\Passport\HasApiTokens Trait 添加到 App\User 模型中 // 提供一些辅助函数检查已认证用户的令牌和使用范围
修改config/auth.php文件
'api' => [
    'driver' => 'passport',
    'provider' => 'users',
    'hash' => false,
],

php artisan migrate
php artisan passport::install 
生成两个数据

这是api.php的路由
Route::post('/register', [PassportController::class, 'register']);

这是控制器代码

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use GuzzleHttp\Exception\RequestException;

class PassportController extends Controller
{ 
    protected $redirectTo = '/home';
    protected $client_id ;
    protected $client_secret ;
    public function __construct()
    {
        // $this->middleware('auth:api', ['except' => ['login','register','refresh']]);

        $client = Cache::remember('passport_client',60*5, function () {
            return DB::table('oauth_clients')->where('password_client',1)->first();
        });

        $this->client_id = $client->id;
        $this->client_secret = $client->secret;


    }

    public function register(Request $request){

        // $this->validator($request->all())->validate();
        // dd(1);
        // $this->create($request->all());
        return $this->getToken();
    }



    protected function create(array $data)
    {    //dd(12);
        // $password = password_hash($data['password']);
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);
    }

    protected function getToken()
    {  
        $http = new \GuzzleHttp\Client;
        try {

            $response=   $http->post('http://www.tt.com/oauth/token', [
                'form_params' => [
                    'grant_type' => 'password',
                    'client_id' => $this->client_id,
                    'client_secret' => $this->client_secret,
                    'username' => '123@123.com',
                    'password' => '123456',
                    'scope' => '',
                ],

                'headers'=>[
                    'Accept'=>'application/json',
                ],
                // 'verify'=>false,

                'http_errors'=>true,
            ]);

            return json_decode((string) $response->getBody(), true);
        }catch(RequestException $e)
        {
            $response = $e->getResponse();
            $message = $e->getMessage();
            dd($response,$message);
        }

    }

}

laravel10 使用passport 密码授权令牌无法使用unsupported_grant_type

看了很多版本的passport 关于password认证的,也尝试过很多次了,但是就是不行,已经快疯了,求各位大佬不吝赐教

《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

password换client_credentials

5个月前 评论
轻语过往 (作者) 5个月前
蜗牛啊蜗牛 (楼主) 5个月前
轻语过往 (作者) 5个月前
轻语过往 (作者) 5个月前
蜗牛啊蜗牛 (楼主) 5个月前
讨论数量: 7

password换client_credentials

5个月前 评论
轻语过往 (作者) 5个月前
蜗牛啊蜗牛 (楼主) 5个月前
轻语过往 (作者) 5个月前
轻语过往 (作者) 5个月前
蜗牛啊蜗牛 (楼主) 5个月前

有点头疼 vendor/laravel/passport/src/PassportServiceProvider.php 的 registerAuthorizationServer 方法里面

file 这里判断是否开启密码授权,然而默认的值是false

file 唯一的开启方法并没有找到调用的地方 enablePasswordGrant

file 强行设置为true后 也是报错不断 解决不动了 报错 在 vendor/league/oauth2-server/src/Grant/PasswordGrant.php 的 respondToAccessTokenRequest 方法 $user = $this->validateUser($request, $client); 这一行,代码实在是看不下去了 头疼 期待大佬解决

5个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!