如何用旧 laravel 护照替换新的 api 访问令牌

How to replace a new api access token with the old one laravel passport

我需要生成访问令牌,而控制器上的方法将通过特定路由调用。我的问题是如何为登录用户生成新的访问令牌并将其替换为旧访问令牌(在 oauth_access_tokens table e.a 中更新用户状态或令牌)。
我需要删除旧的访问令牌并用新的访问令牌替换它。在我的例子中,护照有一个生命周期,每次用户做某事或完成或完成一个动作(例如调用路由等),我需要生成一个新的访问令牌并将其替换为现有的一.

documentation 中非常明确地提到,您需要使用访问令牌来生成刷新令牌,您可以用它替换现有的访问令牌。

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

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

现在您可以采用多种选择:

  • 在使用刷新令牌和新的访问令牌时撤销 已发出
  • 由与令牌关联的用户手动撤销
  • 在预定时间后撤销

查看 oauth_access_tokensoauth_refresh_tokens 表格,这将帮助您了解需要在此处手动更新的内容。