2017-07-15 115 views
0

我設置了Laravel護照,並開始創建幾條獲取路線以獲取一些數據,工作正常。Laravel護照郵遞路線測試郵遞員

現在我想發佈到獲得身份驗證令牌,目前不起作用:

這是我的路,我稱之爲(獲取路線的作品,郵政路線不工作):

Route::group(['middleware' => 'auth:api'], function() 
{; 
    Route::get('users', ['as' => 'users', 'uses' => '[email protected]']); 
    Route::post('login/{id}/{name}', ['as' => 'login', 'uses' => '[email protected]']); 
}); 

在我ApiController的方法目前看起來是這樣的:

public function login(Request $request, $id, $name) 
{ 
    if($request->isMethod('post')) 
    { 
     $id = $request->id; 
     $name = $request->name; 

     $inquiry = new Inquiry(); 
     $inquiry->user_id = $id; 
     $inquiry->user_name = $name; 

     if($inquiry->save()) 
     { 
      return redirect()->route('inquiry.index')->with('success', 'Success.'); 
     } 
     else 
     { 
      return redirect()->route('inquiry.index')->with('error', 'An error accured.')->withInput(); 
     } 
    } 
    else 
    { 
     dd("Use Post."); 
    } 
} 

我試着用下面的選項enter image description here叫它:

編輯

我總算多小時後得到這個工作,但仍然不明白的東西。

起初我以下:

public function callback(Request $request) 
{ 
    dd($request->code) // this holds a token I need for the code parameter in the post 
... 

有了,我能得到的代碼參數的道理,但我認爲這是一個更好的方式來做到這一點。

最後,這是我現在怎麼獲得進入+刷新令牌:

enter image description here

但必須有一個更好的方式來獲取回調請求的代碼標誌($請求 - >碼),而不是傾銷並複製它。

+0

您可以將它保存在會話中或存儲在數據庫中 –

回答

0

問題是你的路由組內有登錄路由,auth:api。這意味着用戶需要進行身份驗證才能進行身份驗證。只要刪除組外的登錄路線,你應該沒問題。

0

您應該在AuthServiceProvider的啓動方法中調用Passport::routes方法。 此方法將註冊需要發出訪問令牌和撤消訪問令牌,客戶和個人訪問令牌的路線

public function boot() 
{ 
    $this->registerPolicies(); 
    Passport::routes(); 
} 

/oauth/authorize路線已經由Passport::routes方法定義。您不需要手動定義此路線。

+0

那麼整個路線如何呢?目前我運行代客所以我的網址是這樣的:'http:// 79f123saf.ngrok.io',所以我嘗試這個URL:'http:// 79f123saf.ngrok.io/api/oauth/authorize'和這個'http:// 79f123saf.ngrok.io/oauth/authorize'的選項我在上面發佈在我的問題中,但是我在'/ oauth/authorize'和'/ api/oauth/authorize'上得到一個'TokenMismatchException'錯誤信息和一個'NotFoundHttpException'因此,我的AuthServiceProvider中有正確的啓動方法 – utdev