2016-12-02 186 views
0

我想連接使用vue和laravel護照生成laravel用戶API,但我不斷收到我的頭文件中的授權錯誤。這ismy代碼Laravel Passport 401使用Apache和Vue的未經授權的錯誤

<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: '[email protected]', 
     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> 

我已經做了研究和回答最多建議修改Apache的配置文件或.htaccess文件,但也似乎並沒有在我結束工作。任何幫助將不勝感激:-)

回答

1

這不是問題的授權檢查上。我將我的Laravel API從Apache移動到nginx,然後工作正常。我更新了我的中間件處理程序。那麼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'); 

      } 
+0

中間件處理程序的名稱是什麼? – user3098538

+0

這是我自己的中間件伴侶 – channasmcs

1

我認爲如果你的vue應用程序和laravel應用程序相結合,那麼你可以使用你的API沒有任何授權頭,你只需要發送X-CSRF-TOKEN併發送該令牌要求沒有必要在vue.js或larvel送來

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

+0

這將是一個自耗API的最佳選擇,但是他們已經通過授權頭有令牌,以便這不會解決問題。 – Dan

0

解析(2) 去 AuthServiceProvider.php

關鍵點:令牌

護照:: tokensExpireIn(碳::設定到期NOW() - > addDays(1));

你必須設置令牌到期 不能是無限的

相關問題