【问题标题】:Is it possible to create Client Tokens while passport-authenticated in Laravel是否可以在 Laravel 中通过护照身份验证时创建客户端令牌
【发布时间】:2020-04-19 04:08:51
【问题描述】:

所以我在 Laravel 的文档 here 和这个问题 here 的指导下制作了一个 API。我遇到的一个问题是,只有通过Auth 进行身份验证才能访问 OAuth。

但是,由于我正在制作的应用程序只是 API,我想知道如何使用 auth:api 作为我的 oauth 链接的中间件,而不是 auth。应该发生的结果是,当我使用Auth:Api 进行身份验证时,我可以创建客户端。

路线:列表

我目前如何验证我的用户(这只是我从文档中获取的参考):

$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' => '',
   ],
]);

return json_decode((string) $response->getBody(), true);

AuthServiceProvider 有:

 Passport::Routes()

最后,是否可以设置个人访问令牌的过期时间?

【问题讨论】:

  • 如果您正在创建一个仅 API 的软件,您可以在 routes/api.php 而不是 routes/web.php 中插入您的路由。这样,您的 API 路由将以 /api 前缀开头,并将使用 auth:api 中间件。
  • Whit I could create clients when I am authenticated 你的意思是在登录时创建新用户?
  • @Stefano 的客户,我的意思是 oauth/clients(客户 ID + Secret)。我了解routes/api,php 的工作原理。我想知道当我以auth:api 而不是auth 进行身份验证时,如何访问诸如 oauth/clients 路由之类的路由

标签: laravel laravel-passport


【解决方案1】:

您是否更改了 config/auth.php 以使用护照进行 API 身份验证?

config/auth.php

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'passport', #Right here
        'provider' => 'users',
    ],
],

https://laravel.com/docs/5.8/passport

【讨论】:

  • 是的,我做到了!我的问题是,当我使用带有令牌的邮递员执行 localhost/oauth/clients 时,它给了我一个路由错误,因为它查找 auth 的登录路由 - 因为 ofc,该路由使用 auth 而不是 auth:api
  • @DeathKing 你能发布你得到的路由错误吗?此外,当您尝试访问 /oauth/clients 时,您是否将标头中的令牌发送为:Authentication : Bearer $token?
  • Symfony\Component\Routing\Exception\RouteNotFoundException Route [login] not defined. Auth 不适用于API,我得到一个401 Unauthenticated Error 另外,我'正在使用 Authorization Bearer [Token] 和 Accept Application/json 作为我的标头。
  • @DeathKing 关于使用内置路由创建客户端,我不确定。但是,您可以为 oauth 客户端创建路由、控制器和模型,并写入“oauth_clients”表以创建新客户端。
猜你喜欢
  • 2019-05-21
  • 1970-01-01
  • 2013-06-28
  • 1970-01-01
  • 2019-12-23
  • 2018-05-28
  • 2016-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多