简体   繁体   English

Laravel Passport 使用自己的 API 和 Axios 401 未认证

[英]Laravel Passport Consuming own API with Axios 401 Unauthenticated

i can't seem to consume my own Laravel API with Axios, i have tried almost all solutions i came across can someone please help me out,我似乎无法使用我自己的 Laravel API 和 Axios,我已经尝试了几乎所有我遇到的解决方案,有人可以帮帮我,

i have properly:我有正确的:

  1. Installed.已安装。
  2. Registered passport service provider.注册护照服务商。
  3. Migrated and generated passport keys.迁移和生成的护照密钥。
  4. Added HasApiTokens trait into my user model.在我的用户 model 中添加HasApiTokens特征。
  5. Added Passport::routes() into AuthServiceProvider.php .Passport::routes()添加到AuthServiceProvider.php中。
  6. Change my config\auth.php .更改我的config\auth.php
  7. Added \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class into last place my web middleware group. \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class到我的 web 中间件组的最后一个位置。 as per the laravel documentation for consuming own api with javascript.根据 laravel 文档,使用 javascript 使用自己的 api。

i have tested it with Axios inside vue components in a web browser normal web login.我已经在 web 浏览器正常 web 登录的 vue 组件中使用 Axios 对其进行了测试。 and through postman: api-login, using the returned token to make api call it works on my development machine.并通过 postman: api-login,使用返回的令牌使 api 调用它在我的开发机器上工作。 But on my production server(Shared Hosting) it keeps returning "Unauthorized" in web browser using web login and using Axios within vue component, and API call in postman returns "Unauthorized" too after login i get a token with i use in making the Postman call. But on my production server(Shared Hosting) it keeps returning "Unauthorized" in web browser using web login and using Axios within vue component, and API call in postman returns "Unauthorized" too after login i get a token with i use in making the Postman 调用。

i checked the request headers are sent too:我检查了请求标头也已发送:

  1. x-csrf-token x-csrf-token
  2. x-xsrf-token x-xsrf-令牌
  3. and also included in the cookie header并且还包含在 cookie header

Please, ensure the following things as you are running laravel on shared hosting.当您在共享主机上运行 laravel 时,请确保以下事项。

  1. Check your root URL set properly with a public folder or You have moved the files of the public folder into the root.检查您的根 URL 是否使用公用文件夹正确设置,或者您已将公用文件夹的文件移动到根目录。
  2. Most importantly check you have.env file & all the files & folders have the proper permissions.最重要的是检查您是否拥有 .env 文件,并且所有文件和文件夹都具有适当的权限。
  3. Also, check all the PHP modules (laravel requirements) that are loaded properly with the proper PHP version.此外,请检查使用正确的 PHP 版本正确加载的所有 PHP 模块(laravel 要求)。
  4. If above steps are correct, then the problem might be your shared server using fcgi rather than regular PHP module.如果上述步骤正确,那么问题可能是您的共享服务器使用 fcgi 而不是常规的 PHP 模块。 Hence, HTTP_AUTHORIZATION failing there.因此,HTTP_AUTHORIZATION 在那里失败。 You can add following codes into the .htaccess file RewriteEngine On RewriteCond %{HTTP:Authorization} ^(.+)$ RewriteRule.* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] that should do HTTP_AUTHORIZATION & solve the problem.您可以将以下代码添加到 .htaccess 文件RewriteEngine On RewriteCond %{HTTP:Authorization} ^(.+)$ RewriteRule.* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]应该执行 HTTP_AUTHORIZATION 并解决问题。

I hope these fix your problem.我希望这些能解决你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM