2017-09-10 74 views
0

我試圖用護照使用的OAuth /授權,以允許Web應用程序以獲取代碼,後來請求令牌,但我得到的錯誤Laravel護照的OAuth /授權錯誤

路線[登錄]未定義

以下是我的代碼。

客戶端代碼

// First route that user visits on consumer app 
Route::get('/', function() { 
    // Build the query parameter string to pass auth information to our request 
    $query = http_build_query([ 
     'client_id' => 3, 
     //'client_secret' => 'MtkEmBL0f0Bf4LcEPcOBUS0wLHvF5xqqchhCpaTH', 
     'redirect_uri' => 'http://client.app:8082/callback', 
     'response_type' => 'code', 
     'scope' => '' 
    ]); 

    // Redirect the user to the OAuth authorization page 
    return redirect('http://server.app:8082/oauth/authorize?' . $query); 
}); 


// Route that user is forwarded back to after approving on server 
Route::get('/callback', function (Request $request) { 
    return 'test 2'; 
    $http = new GuzzleHttp\Client; 

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

    // echo the access token; normally we would save this in the DB 
    return json_decode((string) $response->getBody(), true)['access_token']; 
}); 

oauth/authorize request error

oauth/authorize request error

+0

Accept頭你在哪裏定義權威性路線? –

+0

嗨shukshin!我的路由在AuthServiceProvider File中定義,如下圖所示。Passport :: routes(); –

回答

0

也許你有一個以上的錯誤。看起來你忘了定義常見的驗證路線。從php artisan make:authAuth::routes()開始。 OAuth路由沒有login路由,你得到的錯誤說你沒有定義login路由。它實際上在Auth::routes()中定義。

+0

但是php artisan make:auth會爲用戶登錄,註冊等創建腳手架模板,但它是一個api,所以它不應該有html頁面,而應該改寫授權頁面以便用戶給予權限對於應用程序,所以它可以在用戶許可後請求令牌... –

+0

因此,如果您不需要它的視圖\佈局,只需將'Auth :: routes()'添加到您的路徑文件中即可。 –

+0

等待,如果您安裝了Passport,則表示該用戶可以登錄該網站。登錄後,用戶接受來自其他服務的oauth請求。你如何認爲他接受oauth請求? –

0

我有同樣的問題,顯然,我是不是傳入請求

Accept:application/json 
+0

現在有效嗎?大! –