刷新护照中的令牌

时间:2018-08-07 11:26:39

标签: laravel laravel-passport refresh-token

我是Laravel新手。我正在使用Laravel护照在移动应用程序的API中进行身份验证。现在我想知道如何实现以下功能。

  • 我想在24小时内使我当前的访问令牌失效。
  • 当前访问令牌过期后,我想刷新我的访问令牌。
  • 使用我要访问的刷新的访问令牌。

谁能帮我解决这个问题。我创建了以下代码来生成令牌以及刷新令牌

我的登录功能代码

$client = \Laravel\Passport\Client::where('password_client', 1)->first();
        $request->request->add([
            'grant_type' => 'password',
            'client_id' => $client->id,
            'client_secret' => $client->secret,
            'scope' => null,
            'username' => request('email'),
            'password' => request('password'),
        ]);
        $proxy = Request::create(
                        'oauth/token', 'POST'
        );
        $tokens = \Route::dispatch($proxy);
        $tokrnresponse = (array) $tokens->getContent();
        $tokendata = json_decode($tokrnresponse[0]);

echo $tokendata->access_token 

将为我提供访问令牌,并且

echo $tokendata->refresh_token

会给我刷新令牌 现在从邮递员那里,我尝试使用另一个称为refresh_token()的api刷新令牌。我在标头中传递访问令牌。

refresh_token()代码

$client = \Laravel\Passport\Client::where('password_client', 1)->first();
    $request->request->add([
        'grant_type' => 'refresh_token',
        'client_id' => $client->id,
        'refresh_token' => '<refresh_token>',
        'client_secret' => $client->secret,
        'scope' => null
    ]);
    $proxy = Request::create(
                    'oauth/token', 'POST'
    );
    $tokens = \Route::dispatch($proxy);
    $tokrnresponse = (array) $tokens->getContent();
    $tokendata = json_decode($tokrnresponse[0]);
    print_r($tokendata);
    exit;

我使用上面的代码获得了新的访问令牌,但是当我尝试使用刷新的访问令牌访问其他api时,它给了我“未经身份验证”错误。谁能帮助我。

0 个答案:

没有答案
相关问题