【发布时间】:2022-08-22 18:30:35
【问题描述】:
我是Mojolicious::Plugin::OAuth2 的快乐用户,但有一个问题:我可以毫无问题地获得访问令牌,但我不知道如何获得刷新令牌。文档有点简洁,我在野外找不到示例。
目前我这样做:
plugin OAuth2 => {
providers => {
google => {
key => \'somekey\',
secret => \'somesecret\',
redirect => \'http://localhost:3000/login/google\',
access_type => \'offline\',
scope => join \' \', qw|some scopes|,
}
}
};
get \'/\' => sub {
my $c = shift;
$c->render(template => \'login\');
};
get \'/done\' => sub {
my $c = shift;
$c->render(text => \'done: \' . $c->session(\'token\'));
};
get \'/login/google\' => sub {
my $c = shift;
my $otx = $c->render_later->tx;
my $args = { redirect_uri => \'http://localhost:3000/login/google\' };
$c->oauth2->get_token_p(google => $args)
->then(sub {
my $otx = $otx;
return unless my $res = shift;
$c->session(token => $res->{access_token});
1;
})
->then(sub {
my $tx = shift;
my $ua = $c->app->ua;
my $url = \'https://www.googleapis.com/userinfo/v2/me\';
my $tx = $ua->build_tx(GET => $url);
$tx->req->headers->authorization(\'Bearer \' . $c->session(\'token\'));
return $ua->start_p($tx);
})
->then(sub {
my $tx = shift;
my $otx = $otx;
my $data = $tx->res->json;
$c->app->log->info($tx->res->body);
$c->app->log->info(dumper $tx->res->json);
$c->redirect_to(\'/done\');
})
->catch(sub {
my $err = shift;
$c->log->info($err);
$c->render(text => $err);
});
};
(抱歉转储)这几乎是 Mojolicious::Plugin::OAuth2 的标准流程。
然而,据我所知,来自 Google 的响应不包含任何刷新令牌,我也不知道如何要求 - 在中间的某处插入 $c->oauth2->get_refresh_token_p($provider_name => \\%args); 会给我一个错误的请求响应。
那么,我应该怎么做才能正常工作?
-
您能否提供一个完整的可运行示例?这将有助于澄清您的问题,请参阅minimal reproducible example 了解更多信息
标签: perl oauth-2.0 mojolicious