首页 文章

使用Passport的Laravel API身份验证导致401(未授权)

提问于
浏览
1

根据Laravel关于API路由Passport authentication的文档,我在使用axois从api获取数据时遇到了一些问题 .

到目前为止,我已经通过Composer安装了Passport,添加了hasApiTokens特性,在AuthServiceProvider的引导方法中添加了对Passport :: routes的调用,并将api认证后卫的驱动程序选项设置为“passport” .

由于我希望用户在登录后通过Javascript从基于Vue的SPA使用API,因此我已将CreateFreshApiToken添加到Web中间件组 .

当我运行以下代码片段来测试所有内容时,会出现问题:

axios.get('api/users').then( function(response) { console.log(response.data); });

结果是' Failed to load resource: the server responded with a status of 401 (Unauthorized) '响应 .

我已经检查了使用axois发送的请求中的标头,并且标头包含两者

Cookie:laravel_token=eyJpdiI6ImJ5RG1KRk....

X-XSRF-TOKEN:eyJpdiI6IlVRRHhxSXp2VDVy...

我做错了吗?

2 回答

相关问题