2016-10-14 2 views
3

laravel 5.3의 새로운 oauth2 기능을 사용하여 내 laravel 프로젝트 중 하나에서 다른 API로 호출하려고합니다. 로, 내가 문제없이 호출 할 수있는 미들웨어없이'auth : api'미들웨어를 추가 한 후 경로를 찾을 수 없습니다. Laravel 5.3

Route::get('/hello', function() { 
    return 'hello'; 
})->middleware('auth:api'); 

:

나는이 내가 이전에서 호출 할 나의 새로운 laravel 프로젝트의 api.php 경로 파일의 경로가 미들웨어, 그것은 404 오류를 throw하지 않습니다.

$http = new GuzzleHttp\Client; 

$response = $http->post('http://my-oauth-project.com/oauth/token', [ 
    'form_params' => [ 
     'grant_type' => 'client_credentials', 
     'client_id' => 'client_id', 
     'client_secret' => 'client_secret', 
    ], 
]); 
$token = json_decode($response->getBody(), true)['access_token']; 

$response = $http->get('http://my-oauth-project.com/api/hello', [ 
    'headers' => [ 
     'Accept' => 'application/json', 
     'Authorization' => 'Bearer '.$token, 
    ], 
]); 
return $response->getBody(); 

반환되는 오류 :

[2016-10-14 09:46:14] local.ERROR: exception 'GuzzleHttp\Exception\ClientException' with message 'Client error: `GET http://my-oauth-project.com/api/hello` resulted in a `404 Not Found` response: 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta charset="UTF-8" /> 
     <meta name="robots" content="noindex,nofollow (truncated...) 

답변

3

미들웨어 '인증 : API는'자동으로 리디렉션 여기

는 액세스 토큰을 검색하고 코드가 API를 호출을하다 로그인 페이지에 요청하십시오 (이 경우 존재하지 않으므로 404 오류).

클라이언트 자격 증명 부여에는 로그인이 필요하지 않습니다. 이 문서는 아직 발표되지 않았지만 미들웨어 does exist입니다.

은과 같이 app\Http\Kernel.php$routeMiddleware 변수 아래에 새 미들웨어를 만들 사용하려면

protected $routeMiddleware = [ 
    'client_credentials' => \Laravel\Passport\Http\Middleware\CheckClientCredentials::class, 
]; 

그런 다음 경로의 끝 부분에이 미들웨어를 추가

Route::get('/hello', function() { 
    return 'hello'; 
})->middleware('client_credentials'); 

이가 일 것입니다 나를.

+0

내가 사용하는 미들웨어가 문서가 광범위하게 다루는 무언가 (클라이언트 신임 및 비밀번호 부여)를 위해 부서진다는 것은 상상할 수 없습니다. 관계없이, 이것을 찾아 주셔서 감사합니다! – samrap

관련 문제