Skip to content

Commit

Permalink
- Refresh the access token on POST requests as well.
Browse files Browse the repository at this point in the history
  • Loading branch information
trizen committed Aug 10, 2021
1 parent ff1705e commit 04620e6
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 52 deletions.
83 changes: 49 additions & 34 deletions lib/WWW/YoutubeViewer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -367,34 +367,13 @@ sub _warn_reponse_error {
warn sprintf("[%s] Error occurred on URL: %s\n", $resp->status_line, $url =~ s/([&?])key=(.*?)&/${1}key=[...]&/r);
}

=head2 lwp_get($url, %opt)
Get and return the content for $url.
Where %opt can be:
simple => [bool]
When the value of B<simple> is set to a true value, the
authentication header will not be set in the HTTP request.
=cut

sub lwp_get {
my ($self, $url, %opt) = @_;
sub _request_with_authorization {
my ($self, $block, %opt) = @_;

$url // return;
$self->{lwp} // $self->set_lwp_useragent();
my $response = $opt{simple} ? $block->() : $block->($self->_auth_lwp_header);

if (not defined($self->get_key)) {
return undef if not $opt{simple};
}

my %lwp_header = ($opt{simple} ? () : $self->_auth_lwp_header);
my $response = $self->{lwp}->get($url, %lwp_header);

if ($response->is_success) {
return $response->decoded_content;
if ($response->is_success or $opt{simple}) {
return $response;
}

if ($response->status_line() =~ /^401 / and defined($self->get_refresh_token)) {
Expand All @@ -404,13 +383,14 @@ sub lwp_get {
$self->set_access_token($refresh_token->{access_token});

# Don't be tempted to use recursion here, because bad things will happen!
$response = $self->{lwp}->get($url, $self->_auth_lwp_header);
$response = $block->($self->_auth_lwp_header);

if ($response->is_success) {
$self->save_authentication_tokens();
return $response->decoded_content;
return $response;
}
elsif ($response->status_line() =~ /^401 /) {

if ($response->status_line() =~ /^401 /) {
$self->set_refresh_token(); # refresh token was invalid
$self->set_access_token(); # access token is also broken
warn "[!] Can't refresh the access token! Logging out...\n";
Expand All @@ -429,6 +409,38 @@ sub lwp_get {
}
}

return $response;
}

=head2 lwp_get($url, %opt)
Get and return the content for $url.
Where %opt can be:
simple => [bool]
When the value of B<simple> is set to a true value, the
authentication header will not be set in the HTTP request.
=cut

sub lwp_get {
my ($self, $url, %opt) = @_;

$url // return;
$self->{lwp} // $self->set_lwp_useragent();

if (not defined($self->get_key)) {
return undef if not $opt{simple};
}

my $response = $self->_request_with_authorization(sub { $self->{lwp}->get($url, @_) }, %opt);

if ($response->is_success) {
return $response->decoded_content;
}

$opt{depth} ||= 0;

# Try again on 500+ HTTP errors
Expand All @@ -449,19 +461,22 @@ Post and return the content for $url.
=cut

sub lwp_post {
my ($self, $url, @args) = @_;
my ($self, $url, %opt) = @_;

$self->{lwp} // $self->set_lwp_useragent();

my $response = $self->{lwp}->post($url, @args);
my $response = $self->_request_with_authorization(
sub {
$self->{lwp}->post($url, (exists($opt{headers}) ? $opt{headers} : ()), @_);
},
%opt
);

if ($response->is_success) {
return $response->decoded_content;
}
else {
_warn_reponse_error($response, $url);
}

_warn_reponse_error($response, $url);
return;
}

Expand Down
28 changes: 15 additions & 13 deletions lib/WWW/YoutubeViewer/Authentication.pm
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ sub oauth_refresh_token {

my $json_data = $self->lwp_post(
$self->_get_token_oauth_url(),
[Content => $self->get_www_content_type,
client_id => $self->get_client_id() // return,
client_secret => $self->get_client_secret() // return,
refresh_token => $self->get_refresh_token() // return,
grant_type => 'refresh_token',
]
headers => [Content => $self->get_www_content_type,
client_id => $self->get_client_id() // return,
client_secret => $self->get_client_secret() // return,
refresh_token => $self->get_refresh_token() // return,
grant_type => 'refresh_token',
],
simple => 1,
);

return $self->parse_json_string($json_data);
Expand Down Expand Up @@ -79,13 +80,14 @@ sub oauth_login {

my $json_data = $self->lwp_post(
$self->_get_token_oauth_url(),
[Content => $self->get_www_content_type,
client_id => $self->get_client_id() // return,
client_secret => $self->get_client_secret() // return,
redirect_uri => $self->get_redirect_uri() // return,
grant_type => 'authorization_code',
code => $code,
]
headers => [Content => $self->get_www_content_type,
client_id => $self->get_client_id() // return,
client_secret => $self->get_client_secret() // return,
redirect_uri => $self->get_redirect_uri() // return,
grant_type => 'authorization_code',
code => $code,
],
simple => 1,
);

return $self->parse_json_string($json_data);
Expand Down
8 changes: 3 additions & 5 deletions lib/WWW/YoutubeViewer/Videos.pm
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ sub videos_from_category {
}

my $videos = $self->_get_results(
$self->_make_videos_url(
chart => 'mostPopular',
videoCategoryId => $cat_id,
)
$self->_make_videos_url(chart => 'mostPopular',
videoCategoryId => $cat_id,)
);

if (not $yv_utils->has_entries($videos)) {
Expand Down Expand Up @@ -158,7 +156,7 @@ sub send_rating_to_video {

if ($rating eq 'none' or $rating eq 'like' or $rating eq 'dislike') {
my $url = $self->_simple_feeds_url('videos/rate', id => $video_id, rating => $rating);
return defined($self->lwp_post($url, $self->_auth_lwp_header()));
return defined($self->lwp_post($url));
}

return;
Expand Down

0 comments on commit 04620e6

Please sign in to comment.