在听了很多关于laravel passport的事情后,我考虑将其应用到我的新项目中,我的要求是创建一个将在移动应用程序中使用的API.

So my mobile app is a client, which will further have its users.

I followed the steps mentioned by Taylor and also read about it here. In a nutshell I followed these steps:

  1. 已安装laravel/passport个.
  2. Created a website user.
  3. 生成的Passport 密钥php artisan passport:install
  4. Generated client_id and client_secret using php artisan passport:client
  5. Added redirection and callback routes in web.php
  6. 授权用户并获得最终访问令牌.

Then I tried calling api/user( with Header Authorization containing value Bearer eyJ0eXAiOiJKV1...(token)

我收到了数据.非常简单和整洁.

但我的应用程序用户不会知道这些细节.所以我想配置Password Grant Tokens个完全符合我的要求.

Now starts the real headache. I've been trying to set this up for the last 3 days and continuously getting

{"error":"invalid_client","message":"Client authentication failed"}

我已经try 了几乎所有我在网上遵循的指南:Redirection IssuesAdd Atleast One Scope SolutionP100Y Issue等等.

But I'm still getting invalid client error. Here's what I'm passing through POSTMAN to oauth/token:

{
    "grant_type": "password,"
    "client_id": "3,"
    "client_secret": "8BUPCSyYEdsgtZFnD6bFG6eg7MKuuKJHLsdW0k6g,"
    "username": "test@gmail.com,"
    "password": "123456,"
    "scope": ""
}

Any help would be appreciated.

推荐答案

Check your credentials first if they are correct, Secondly check your model table which uses \Laravel\Passport\HasApiTokens trait that whether it contains email column, because by default it is used to identify user when validating credentials. if your table has username column or any other column which is used in validating credentials you must define a function findForPassport in that model. like this,

public function findForPassport($username) {
       return self::where('username', $username)->first(); // change column name whatever you use in credentials
    }

I use username and password column to validate a user, in {project_directory}\vendor\laravel\passport\src\Bridge\UserRepository.php

此函数用于验证您的凭据,

public function getUserEntityByUserCredentials($username, $password, $grantType, ClientEntityInterface $clientEntity)
    {
        if (is_null($model = config('auth.providers.users.model'))) {
            throw new RuntimeException('Unable to determine user model from configuration.');
        }

        if (method_exists($model, 'findForPassport')) { // if you define the method in that model it will grab it from there other wise use email as key 
            $user = (new $model)->findForPassport($username);
        } else {
            $user = (new $model)->where('email', $username)->first();
        }

        if (! $user || ! $this->hasher->check($password, $user->password)) {
            return;
        }

        return new User($user->getAuthIdentifier());
    }

notice the second if statement and you will get to know what is happening there.

希望这对您有所帮助:)

Laravel相关问答推荐

子目录中具有Vue实例的Laravel不起作用

如何从@foreach 循环中捕获所有数据并将其传递给 值? - Laravel

使用Laravel Blade Formatter和PHP Intelephense进行 VSCode 格式化

判断 Laravel 中是否存在唯一索引键

Laravel 5 - 手动分页

Laravel Request::input 调用未定义的方法

控制器中的 Laravel Auth 用户 ID

如何卸载 Laravel?

Laravel 随机排序

为什么客户凭证应该与 Laravel Passport 中的用户相关联?

验证匹配字符串

为什么 BroadCastEvent 在 Laravel 中排队?如何阻止它?

在 Laravel 测试用例中模拟一个 http 请求并解析路由参数

Blade 中的条件扩展

Laravel 检测手机/平板电脑并加载正确的视图

Laravel 5 - 跳过迁移

Laravel 5 new auth:获取当前用户以及如何实现角色?

Laravel 部署……有标准的方式吗?

错误:找不到模块 'webpack/lib/rules/DescriptionDataMatcherRulePlugin' 需要堆栈:

你如何在 Laravel 5.3 的新通知服务生成的Electron邮件中添加图像?