Laravel 护照 invalid_grant 密码 grant_type

Laravel Passport invalid_grant for password grant_type

我一直在尝试为我的 api 创建一个 access_token。我已经 followed the setup 并且正在使用 Postman 来 test/create 一个令牌。我似乎无法克服 invalid_grant 错误。

我已经尝试了我能找到的每一种组合,但没有任何运气。这是我的设置:

发送 POST 请求至:http://mywebsite.local/oauth/token

在 body 中,我将 form-data 设置为此 (name/value):

grant_type      password
client_id       1
client_secret   <super_long_string>
username        my@email.com
password        secret

我用 tinker 创建了一个虚拟用户:

factory('App\User')->create()

我在上面的username/password中使用新创建的用户。

不管我在做什么(除了不传递任何东西)这是我经常看到的错误:

{
    "error": "invalid_grant",
    "error_description": "The provided authorization grant (e.g., authorization code, resource owner credentials) or refresh token is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client.",
    "hint": "",
    "message": "The provided authorization grant (e.g., authorization code, resource owner credentials) or refresh token is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client."
}

我读过很多遍,这意味着 grant_type 我尝试获取的内容与客户不匹配。我正在使用 php artisan passport:client --password 创建客户端,所以我不明白为什么它无效。我只有一个客户端,所以我知道我使用的是正确的 idThis issue 好像是同一件事,我看到了,但后来被关闭了。

对于我的headers,我只设置了Content-Type application/json,我没有为授权headers设置任何东西。

我不确定还能尝试什么。感谢您的任何建议!

here所述,从 5.8 版本开始,默认密码 "secret" 已更新为 "password"。所以你输入的是旧密码。

尝试为用户散列密码。 起初我认为密码至少需要 8 个字符长。 然后,例如如果您使用“test1234”作为创建用户的密码,请转到您的终端并输入:

>> php artisan tinker

>> echo bcrypt('test1234')

复制输出并将其保存在您创建的用户而不是 test1234 的密码数据库列中。现在你对 http://mywebsite.local/oauth/token 的 post 请求应该有效了。

我在 laravel 8 中遇到了同样的问题,我通过重新安装 laravel passport

解决了这个问题

这些是步骤:

1- 在AuthServiceProvider.php

中评论这一行
if (! $this->app->routesAreCached()) {       
   //Passport::routes();
}

2- 在 composer.json 中删除以下行:

 "laravel/passport": "^xx.x",

3- 运行 这些命令:

composer dump-autoload
composer update
php artisan migrate:fresh

现在我们要安装laravel passport 4- 运行 这些命令:

composer require laravel/passport
php artisan migrate
php artisan passport:install --force

5- 在 AuthServiceProvider.php 中添加这些行:

use Laravel\Passport\Passport;
class AuthServiceProvider extends ServiceProvider
{
    protected $policies = [
        'App\Models\Model' => 'App\Policies\ModelPolicy',
    ];

    public function boot()
    {
        $this->registerPolicies();

        if (! $this->app->routesAreCached()) {
            
            Passport::routes();
        }
    }
}

6- 在 config\auth.php 添加这些代码:

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

完成,经过这些步骤,laravel-passport运行良好