2017-07-17 24 views

回答

0

如果你正確設置Laravel護照,你應該有這個在您的視圖:enter image description here

你需要創建一個客戶端,該客戶端具有客戶端ID和客戶端密鑰。

現在您需要打開您的客戶應用程序,其中應包含您的客戶端ID和客戶端密碼。

它看起來像這樣(你必須標記和編號更改爲特定的一個):

class OAuthController extends Controller 
{ 
    public function redirect() 
    { 
     $query = http_build_query([ 
      'client_id' => 3, 
      'redirect_uri' => 'http://localhost/app/public/callback', 
      'response_type' => 'code', 
      'scope' => '', 
     ]); 

     return redirect('http://localhost/app/public/oauth/authorize?' . $query); 
    } 

    public function callback(Request $request) 
    { 
     $http = new Client; 

     $response = $http->post('http://localhost/app/public/oauth/token', [ 
      'form_params' => [ 
       'grant_type' => 'authorization_code', 
       'client_id' => 3, // from admin panel above 
       'client_secret' => 'BcTgzw6YiwWUaU8ShX4bMTqej9ccgoA4NU8a2U9j', // from admin panel above 
       'redirect_uri' => 'http://localhost/app/public/callback', 
       'code' => $request->code // Get code from the callback 
      ] 
     ]); 

     return json_decode((string) $response->getBody(), true); 
    } 
} 

現在你需要調用消費者應用程序,並授權您的應用程序。

enter image description here

如果工作你會得到一個訪問令牌+刷新令牌。

它應該是這樣的:

enter image description here

現在你可以使用像郵遞員程序進行測試。

基本上你打電話給你的get路線,並添加訪問令牌,它給你訪問API,就像這樣:

enter image description here

如果您有任何其它問題我建議你閱讀docs

因此,我強烈建議您觀看Taylor Otwell的video

當然,如果您還有其他問題,您可以給我評論。

+0

我跟着視頻toturial,並得到了你所顯示的屏幕,但仍然出現錯誤。 –

+0

哪個錯誤,因此你不應該只是把答案放在這裏,而不是真的回答相反的意見,否則人們會downvote你的問題,你可能不會得到幫助了 – utdev

0

我有我的回答是:內/resources/assets/js/bootstrap.js

添加以下代碼:window.axios.defaults.headers.common = { 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content'), 'X-Requested-With': 'XMLHttpRequest' };

完美的工作......

0

超越護照集成和安裝的CSRF標題問題。

一個可能的解決方案是將中間件定義從Route聲明移到控制器構造函數。

這樣的:

Route::middleware('auth:api', 'throttle:60,1')->group(function() { 
    Route::get('tasks', 'Api\[email protected]')->name('tasks'); 
}); 

然後去除auth中間件後:

Route::middleware('api', 'throttle:60,1')->group(function() { 
    Route::get('tasks', 'Api\[email protected]')->name('tasks'); 
}); 

,並設置它的控制器構造函數:

<?php 
namespace App\Http\Controllers\Api; 
use App\Http\Controllers\Controller; 
class TasksController extends Controller 
{ 
    function __construct() 
    { 
     $this->middleware('auth'); 
    } 
    public function index() { // ... 
    } 

我很抱歉沒能解釋如何或爲什麼這個調整工作。希望我會稍後更新此更好的解釋..