【问题标题】:Laravel Passport column \"api_token\" does not existLaravel Passport 列“api_token”不存在
【发布时间】:2018-07-10 07:47:41
【问题描述】:

我正在一个 api 项目上设置 laravel 护照。我尝试按照this site 上的步骤操作,但无法进行身份验证。

请求令牌部分似乎工作正常。当调用http://127.0.0.1:8000/oauth/token 时,它会返回一个有效的令牌。

当我使用授权标头中的令牌向 api 发送请求时,它会给出一列“api_token”不退出错误

Authorization Header: Authorization:Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1Ni........

错误:

"SQLSTATE[42703]: Undefined column: 7 ERROR:  column "api_token" does not exist↵LINE 1: select * from "users" where "api_token" = $1 limit 1↵  

我需要自己创建 api_token 列吗?我使用默认的迁移文件来创建表。这是用户表的迁移文件

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

【问题讨论】:

  • 我创建了一个新项目并从那里重新开始。它工作正常。

标签: laravel laravel-passport


【解决方案1】:

解决方案是php artisan config:cache(不要忘记在config 目录发生任何更改后执行此操作)。

【讨论】:

    【解决方案2】:

    用这个更改 config/auth.php 文件的guards

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

    更改 auth.php 后,清除所有缓存。

    【讨论】:

      【解决方案3】:

      你的问题出在 config/auth.php

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

      我在这里附上 auth.php 的完整代码

      <?php
      
      return [
      
          /*
          |--------------------------------------------------------------------------
          | Authentication Defaults
          |--------------------------------------------------------------------------
          |
          | This option controls the default authentication "guard" and password
          | reset options for your application. You may change these defaults
          | as required, but they're a perfect start for most applications.
          |
          */
      
          'defaults' => [
              'guard' => 'web',
              'passwords' => 'users',
          ],
      
          /*
          |--------------------------------------------------------------------------
          | Authentication Guards
          |--------------------------------------------------------------------------
          |
          | Next, you may define every authentication guard for your application.
          | Of course, a great default configuration has been defined for you
          | here which uses session storage and the Eloquent user provider.
          |
          | All authentication drivers have a user provider. This defines how the
          | users are actually retrieved out of your database or other storage
          | mechanisms used by this application to persist your user's data.
          |
          | Supported: "session", "token"
          |
          */
      
          'guards' => [
              'api' => [
                  'driver' => 'passport',
                  'provider' => 'users',
              ],
              'web' => [
                  'driver' => 'session',
                  'provider' => 'users',
              ],
          ],
      
          /*
          |--------------------------------------------------------------------------
          | User Providers
          |--------------------------------------------------------------------------
          |
          | All authentication drivers have a user provider. This defines how the
          | users are actually retrieved out of your database or other storage
          | mechanisms used by this application to persist your user's data.
          |
          | If you have multiple user tables or models you may configure multiple
          | sources which represent each model / table. These sources may then
          | be assigned to any extra authentication guards you have defined.
          |
          | Supported: "database", "eloquent"
          |
          */
      
          'providers' => [
              'users' => [
                  'driver' => 'eloquent',
                  'model' => App\Models\User::class,
              ],
      
      
          ],
      
          /*
          |--------------------------------------------------------------------------
          | Resetting Passwords
          |--------------------------------------------------------------------------
          |
          | You may specify multiple password reset configurations if you have more
          | than one user table or model in the application and you want to have
          | separate password reset settings based on the specific user types.
          |
          | The expire time is the number of minutes that the reset token should be
          | considered valid. This security feature keeps tokens short-lived so
          | they have less time to be guessed. You may change this as needed.
          |
          */
      
          'passwords' => [
              'users' => [
                  'provider' => 'users',
                  'table' => 'password_resets',
                  'expire' => 60,
              ],
          ],
      
      ];
      

      【讨论】:

        【解决方案4】:

        解决方法很简单!

        转到文件:config/auth.php

        识别此代码块:

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

        并更改为:

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

        【讨论】:

          【解决方案5】:

          在您的用户迁移中,您必须添加 api_token

          public function up()
          {
              Schema::create('users', function (Blueprint $table) {
                  $table->increments('id');
                  $table->string('name');
                  $table->string('email')->unique();
                  $table->string('password');
                  $tabke->string('api_token'); // specify the length also
                  $table->rememberToken();
                  $table->timestamps();
              });
          }
          

          【讨论】:

          • 我刚刚尝试过,并将字段的长度设置为 60。错误已经消失,但是当我向 api 发出请求时,它给出了 401 Unauthenticated 错误。我在数据库中注意到这个 api_token 字段是空白的。这个字段有什么用?
          • 您的 ajax 请求中需要一个身份验证承载
          猜你喜欢
          • 2020-02-26
          • 2016-11-10
          • 2017-07-23
          • 2020-01-08
          • 2018-10-11
          • 2017-01-17
          • 1970-01-01
          • 2020-07-23
          • 2017-07-29
          相关资源
          最近更新 更多