Authentication Laravel API令牌身份验证不工作
我正在尝试使用api身份验证令牌对用户进行身份验证。但即使使用了正确的令牌,它也会说“未经授权”。 我正在使用laravel auth和socialite进行社会认证 所以我的api.php路径是这样的Authentication Laravel API令牌身份验证不工作,authentication,laravel-5,oauth-2.0,laravel-passport,laravel-socialite,Authentication,Laravel 5,Oauth 2.0,Laravel Passport,Laravel Socialite,我正在尝试使用api身份验证令牌对用户进行身份验证。但即使使用了正确的令牌,它也会说“未经授权”。 我正在使用laravel auth和socialite进行社会认证 所以我的api.php路径是这样的 Route::group(['middleware'=>'auth:api'], function(){ Route::get('hello','ApiTestControler@index'); }); 我正试图通过这个url访问它 http://localhost:8000/
Route::group(['middleware'=>'auth:api'], function(){
Route::get('hello','ApiTestControler@index');
});
我正试图通过这个url访问它
http://localhost:8000/api/hello
在标题中
token: TOKEN HERE
Content-Type: application/json
Accept: application/json
我希望它能让我登录并显示ApitesController索引方法
但这是一个错误
如何解决此问题并使用API令牌进行用户身份验证
我的控制器
class ApiTestController extends Controller
{
public function index(){
return json_encode ("Welcome REST API");
}
}
用户迁移表
$table->increments('id');
$table->string('name')->unique();
$table->string('first_name')->nullable();
$table->string('last_name')->nullable();
$table->string('email')->unique()->nullable();
$table->string('password');
$table->rememberToken();
$table->boolean('activated')->default(false);
$table->string('token');
$table->ipAddress('signup_ip_address')->nullable();
$table->ipAddress('signup_confirmation_ip_address')->nullable();
$table->ipAddress('signup_sm_ip_address')->nullable();
$table->ipAddress('admin_ip_address')->nullable();
$table->ipAddress('updated_ip_address')->nullable();
$table->ipAddress('deleted_ip_address')->nullable();
$table->timestamps();
$table->softDeletes();
以及config\auth.php中的auth配置
我发现唯一可能导致问题的是数据库中令牌列的名称,它应该是api_令牌,因此将迁移更改为:
$table->increments('id');
$table->string('name')->unique();
$table->string('first_name')->nullable();
$table->string('last_name')->nullable();
$table->string('email')->unique()->nullable();
$table->string('password');
$table->rememberToken();
$table->boolean('activated')->default(false);
$table->string('api_token', 60)->unique(); //<-- this one here
$table->ipAddress('signup_ip_address')->nullable();
$table->ipAddress('signup_confirmation_ip_address')->nullable();
$table->ipAddress('signup_sm_ip_address')->nullable();
$table->ipAddress('admin_ip_address')->nullable();
$table->ipAddress('updated_ip_address')->nullable();
$table->ipAddress('deleted_ip_address')->nullable();
$table->timestamps();
$table->softDeletes();
而不是:
Auth::user()
我发现唯一可能导致问题的是数据库中令牌列的名称,它应该是api_令牌,因此将迁移更改为:
$table->increments('id');
$table->string('name')->unique();
$table->string('first_name')->nullable();
$table->string('last_name')->nullable();
$table->string('email')->unique()->nullable();
$table->string('password');
$table->rememberToken();
$table->boolean('activated')->default(false);
$table->string('api_token', 60)->unique(); //<-- this one here
$table->ipAddress('signup_ip_address')->nullable();
$table->ipAddress('signup_confirmation_ip_address')->nullable();
$table->ipAddress('signup_sm_ip_address')->nullable();
$table->ipAddress('admin_ip_address')->nullable();
$table->ipAddress('updated_ip_address')->nullable();
$table->ipAddress('deleted_ip_address')->nullable();
$table->timestamps();
$table->softDeletes();
而不是:
Auth::user()
乐意帮忙;乐意帮忙;