编程语言
首页 > 编程语言> > php-Laravel 5.3密码授予令牌[用户凭证错误]

php-Laravel 5.3密码授予令牌[用户凭证错误]

作者:互联网

使用流浪汉/宅基地设置Laravel的5.3护照.跟着直到我达到Password Grant Tokens

在这里,我找到了这个GuzzleHttp片段以发布到/ oath / token:

    Route::get('/api_grant', function(){
    $http = new GuzzleHttp\Client;

    $response = $http->post('http://mavrik-cms.io/oauth/token', [
        'form_params' => [
            'grant_type' => 'password',
            'client_id' => '6',
            'client_secret' => 'BBlhjUlGsbde5zQ3LBHAr6inJoQVFOMIZlR1RFUI',
            'username' => 'mrricki.m.usmc@gmail.com',
            'password' => 'secret',
            'scope' => '',
        ],
    ]);

    return json_decode((string) $response->getBody(), true);
});

尽管我已经发送了所有必需和正确的信息,但我仍会收到:(注意:我是唯一的用户,因此我的电子邮件和虚拟密码以及客户端已正确关联到oauth客户端表上的user_id,并且password_client设置为1 );

{"error":"invalid_credentials","message":"The user credentials were incorrect."}

enter image description here

我已经把这个弄乱了几个小时.我尝试了所有可能的用户名和密码组合.(来自用户模型的电子邮件作为用户名,来自用户模型的用户作为用户名,用户模型中的密码作为密码,等等.)

解决方法:

我遇到了同样的问题,由于我在表中使用用户名字段而不是默认电子邮件进行身份验证,所以最终发生了该问题.

为了适应这种行为,我不得不在用户雄辩的模型中重写findForPassport().护照的UserRepository默认为电子邮件

if (method_exists($model, 'findForPassport')) {
    $user = (new $model)->findForPassport($username);
} else {
    $user = (new $model)->where('email', $username)->first();
}

不知道这是否有帮助,但这对我有用.

public static function findForPassport($username)
{
    return User::where('username', $username)->first();
}

标签:laravel,oauth-2-0,laravel-passport,php
来源: https://codeday.me/bug/20191112/2023986.html