阅读(1089) (4)

Laravel 8 请求令牌

2021-07-09 12:59:38 更新

创建密码授权的客户端后,就可以使用用户的电子邮件地址和密码向 /oauth/token 路由发出 POST 请求来获取访问令牌。而该路由已经由 Passport::routes 方法注册,因此不需要手动定义它。如果请求成功,会在服务端返回的 JSON 响应中收到一个 access_tokenrefresh_token

$http = new GuzzleHttpClient;

$response = $http->post('http://your-app.com/oauth/token', [

    'form_params' => [

        'grant_type' => 'password',

        'client_id' => 'client-id',

        'client_secret' => 'client-secret',

        'username' => '[email protected]',

        'password' => 'my-password',

        'scope' => '',

    ],

]);

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

技巧:默认情况下,访问令牌是长期有效的。你可以根据需要配置访问令牌的有效时间