gpt4 book ai didi

laravel - 如何在 Laravel Passport 中获取刷新 token ?

转载 作者:行者123 更新时间:2023-12-03 15:09:01 24 4
gpt4 key购买 nike

我正在使用 Laravel 6.7 并尝试使用 护照用于用户身份验证。

我可以在用户注册时为他们创建访问 token 。这是代码:

$user = User::create($input);
$user->createToken('auth-token');

正如我在 中定义的那样,此访问 token 的有效期为 15 分钟。 AuthServiceProvider.php 文件 boot()功能如下:
Passport::personalAccessTokensExpireIn(Carbon::now()->addMinutes(15));

我想使用刷新 token 刷新它,但似乎无法理解如何。

我到处找(包括 Laravel 网站),他们都告诉我这样做:
$http = new GuzzleHttp\Client;

$response = $http->post('http://your-app.com/oauth/token', [
'form_params' => [
'grant_type' => 'refresh_token',
'refresh_token' => 'the-refresh-token',
'client_id' => 'client-id',
'client_secret' => 'client-secret',
'scope' => '',
],
]);

没有任何关于值 'the-refresh-token' 的明确解释, 'client-id''client-secret'是注定的。

answer关于堆栈溢出说如下:

you must send old refresh-token ('refresh_token' => 'the-refresh-token') and this code produces a new token and refresh-refresh.



但是我没有刷新 token ,我正在尝试创建一个。我只是创建一个随机字符串吗?

最佳答案

使用密码授予 token
发件人:Laravel Docs: Password Grant Tokens

The OAuth2 password grant allows your other first-party clients, such as a mobile application, to obtain an access token using an e-mail address / username and password.


首先,您需要通过以下方式生成密码授予客户端:
php artisan passport:client --password
这会给你一个 client_id和一个 client_secret 所以接下来您可以在您的移动应用程序中实现您的登录,如下所示:
POST
http://your-app.com/oauth/token

Body:
{
'grant_type' => 'password',
'client_id' => 'client-id',
'client_secret' => 'client-secret',
'username' => 'taylor@laravel.com',
'password' => 'my-password',
'scope' => '',
}
结果如下:
{
"token_type": "Bearer",
"expires_in": 300,
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjAyMGM1YTQ2MzM5ZTAxNjBjODViOWUyMGE0YTAxYzhmOWYzOTZkYjlhMmM1OWZiNjk0NDVjZTZlYTIyMmUyZmEyNDZmYzQ0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608dcfc120596c2ed72ffe7dd0a1db3bc7511ec905a65c63551239581a1c13c3c0b53fd0f8db97d2b49763f5bd98b7624a432d7e82161cc9e543d3e2550f73d6bcea0014aa0d4f72c2eb3edfe2f256fae1a8ea69270735be98a85b7040e33194eb449187b67aa0dbb10f75bdf620b6416c12756e96449e92aa6ad7b56be53876113a9d17d93c1039f54647040ce8acd7f242684d0b0aa1835267dbc6bab87c6b2a7862ca3bcd8396ae7c912b03ee3df7d471f74b96d0c48ec76a55ff05762227722ffb99ecb12f30fae9042b078383678492ef73e5fedfb0085a30a9511bc94588edd5a171e0650a092bb9c37e7571aeb6d0b6d9048189ab0fa16d48477e6d51e81efbe762af34c46ed2da1862528c24d00f0139f3e74eccbefd06a7dd238ccff85b9cbec68e2e7483a9fb2f4bc314d81d48f3dbbe0a9c8b42bd76bc4ad57fa2afad52092c5339f9461"
}
如果您想刷新过期的 token ,您将向 发出请求。 /oauth/tokenPOST方法
和 body :
{
'grant_type' => 'refresh_token',
'refresh_token' => 'the-refresh-token',
'client_id' => 'client-id',
'client_secret' => 'client-secret',
'scope' => '',
}
把以前的 refresh_token , client_id , client_secret您将收到另一个包含其他 token 的响应

关于laravel - 如何在 Laravel Passport 中获取刷新 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59330402/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com