简体   繁体   English

使用Apache和Vue的Laravel Passport 401未经授权的错误

[英]Laravel Passport 401 Unauthorized Error using Apache and Vue

i am trying to connect a generate a laravel user API using vue and laravel passport but i keep getting an authorization error in my headers . 我正在尝试使用vue和laravel通行证连接生成laravel用户API,但我的标头中始终出现授权错误。 This ismy code 这是我的代码

<script>
import Hello from './components/Hello'

export default {
  name: 'app',

  components: {
    Hello
  },
  created () {
    const postData = {
      grant_type: 'password',
      client_id: 2,
      client_secret: 'sXdg5nOO4UU2muiHaQnTq4hDQjyj17Kd9AeKuNEx',
      username: 'robertrutenge@gmail.com',
      password: 'password',
      scope: ''

    }
    this.$http.post('http://localhost:8000/oauth/token', postData)

   .then(response => {
     console.log(response)

     const header = {
       'Accept': 'application/json',
       'Authorization': ~'Bearer ' + response.body.access_token
     }
     this.$http.get('http://localhost:8000/api/user', {headers: header})

     .then(response => {
       console.log(response)
     })
   })
  }
}
</script>

I have done a research and most answers suggest modifying apache config file or .htaccess file but that also does not seem to work on my end . 我已经进行了一项研究,大多数答案都建议修改apache配置文件或.htaccess文件,但这似乎对我没有用。 Any help will be appreciated :-) 任何帮助将不胜感激 :-)

i think if your vue app and laravel app is combine, then you can use your api without any authorization header you just need to send X-CSRF-TOKEN and send that token with each request no need to send authorization check here 我认为,如果您的vue应用程序和laravel应用程序结合使用,那么您可以使用不带任何授权标头的api,您只需要发送X-CSRF-TOKEN并随每个请求发送该令牌,而无需在此处发送授权检查

https://laravel.com/docs/5.3/passport#consuming-your-api-with-javascript https://laravel.com/docs/5.3/passport#taking-your-api-with-javascript

this is not problem on in vue.js or larvel. 在vue.js或larvel中这不是问题。 i moved my l Laravel API from Apache to nginx then working fine. 我将我的Laravel API从Apache移到了Nginx,然后工作正常。 i updated my middleware handler like this. 我这样更新了我的中间件处理程序。 then working fine on Apache server 然后在Apache服务器上正常工作

  $origin = $request->server()['HTTP_ORIGIN'];

            if(in_array($origin, $url)){
                header('Access-Control-Allow-Origin: '. $origin);
                header('Access-Control-Allow-Headers: Origin, Content-Type, Accept, Authorization, X-Csrf-Token'); 

            }

resolve (2) go to AuthServiceProvider.php 解决(2)转到AuthServiceProvider.php

keypoint:set expiration of token 关键点:设置令牌到期

Passport::tokensExpireIn(Carbon::now()->addDays(1)); Passport :: tokensExpireIn(Carbon :: now()-> addDays(1));

You must to set token expiration cant be infinite 您必须将令牌过期设置为无限

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

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