首页 文章

始终响应401(未授权)我的Laravel Passport生成的OAuth 2令牌

提问于
浏览
0

我在用:

  • 用于REST检查的Postman / Insomnia

  • Laravel 5.6与Laravel Passport

  • Vagrant(Apache 2,PHP 7.2)

制作了Laravel Docs for Laravel Passport上描述的所有清单,并且在某些步骤之后,我收到了有效OAuth访问令牌的HTTP 401 .

  • 由/ oauth / token /请求带有client_id和client_secret的新访问令牌 .

  • 使用收到的访问令牌来授权我的简单Laravel REST测试控制器,其中包含Oauth api中间件 .

  • 结局是一个:401未经授权:(

所以,这是我的一些配置:

Apache
enter image description here

api route
enter image description here

Kernel.php
enter image description here

PassportServiceProvider.php
enter image description here

AuthServiceProvider.php
enter image description here

1 回答

  • 1

    我也有一个非常相似的问题:

    您和我的代码之间的区别:在 routes/api.php 中,我只使用 auth:api . 我没有在app文件夹中创建 PassportServiceProvider.php . 在 Kernel.php 矿是 client 不是 client_credentials .

    我在POST请求调用中使用 client_credentials 作为 grant_type . 结果我总是得到401 .

    在我使用密码授予客户端创建用户之前:

    php artisan passport:client --password
    

    并在POST请求调用中将 client_credentials 更改为 password

    $http = new GuzzleHttp\Client;
    
    $response = $http->post('http://your-app.com/oauth/token', [
        'form_params' => [
            'grant_type' => 'password',
            'client_id' => 'client-id',
            'client_secret' => 'client-secret',
            'username' => 'taylor@laravel.com',
            'password' => 'my-password',
            'scope' => '',
        ],
    ]);
    
    $access_token = json_decode((string) $response->getBody(), true)['access_token'];
    

    将访问令牌放在标头的承载中,它可以工作 . 而且你也可以使用 $request->user(); 获得当前用户

    如果您使用 client_credentials 作为 grant_type ,它将通过客户端中间件,因此需要删除中间件 auth:api .

相关问题