添加'auth:api'中间件Laravel 5.3后找不到路由 [英] Route not found after adding 'auth:api' middleware Laravel 5.3

查看:367
本文介绍了添加'auth:api'中间件Laravel 5.3后找不到路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用laravel 5.3中的新oauth2功能从我的一个laravel项目中进行到另一个的api调用.

我要从旧的laravel项目调用的新laravel项目的api.php路由文件中有此路由:

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

在没有中间件的情况下,我可以毫无问题地调用它,而在中间件中,它会引发404 not found错误.

以下是检索访问令牌然后进行api调用的代码:

$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...)

解决方案

中间件'auth:api'自动将请求重定向到登录页面(在这种情况下不存在,因此出现404错误).

客户端凭据授予不需要登录.它的文档尚未发布,但是中间件确实存在.

>

要使用它,请在app\Http\Kernel.php中的$routeMiddleware变量下创建一个新的中间件,如下所示:

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

然后将此中间件添加到路由的末尾:

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

这对我有用.

I'm trying to do an api call from one of my laravel projects to another using the new oauth2 feature in laravel 5.3.

I have this route in the api.php route file of my new laravel project that I want to call from the old one:

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

Without the middleware I can call it without a problem, with the middleware, it throws a 404 not found error.

Here is the code that retrieves the access token and then makes the api call:

$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();

The error that is returned:

[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...)

解决方案

The middleware 'auth:api' automatically redirects the request to a login page (which in this case doesn't exist, hence the 404 error).

The client credentials grant doesn't require a login. The documentation for it hasn't been published yet but the middleware does exist.

To use it create a new middleware under the $routeMiddleware variable in app\Http\Kernel.php like so:

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

Then add this middleware onto the end of the route:

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

This is what worked for me.

这篇关于添加'auth:api'中间件Laravel 5.3后找不到路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆