Files
mschuepbach 8c0319f293 Add OAuth
2024-03-27 15:25:17 +01:00

62 lines
1.2 KiB
Perl

package YTMusicAPI::Auth::OAuth::Token;
use strict;
use warnings;
use JSON;
sub new {
my ( $class, $args ) = @_;
my $self = {
scope => $args->{scope} // "https://www.googleapis.com/auth/youtube",
token_type => $args->{token_type} // "Bearer",
access_token => $args->{access_token} // undef,
refresh_token => $args->{refresh_token} // undef,
expires_at => $args->{expires_at} // 0,
expires_in => $args->{expires_in} // 0,
};
bless $self, $class;
return $self;
}
sub members {
return qw(
scope
token_type
access_token
refresh_token
expires_at
expires_in
);
}
sub repr {
my ($self) = @_;
return ref($self) . ": " . $self->as_json();
}
sub as_dict {
my ($self) = @_;
my %copy;
@copy{ $self->members() } = @{$self}{ members() };
return \%copy;
}
sub as_json {
my ($self) = @_;
my $dict = $self->as_dict();
return encode_json($dict);
}
sub as_auth {
my ($self) = @_;
return $self->{token_type} . " " . $self->{access_token};
}
sub is_expiring {
my ($self) = @_;
return $self->{expires_in} < 60 ? 1 : 0;
}
1;