Add OAuth
This commit is contained in:
61
YTMusicAPI/Auth/OAuth/Token.pm
Normal file
61
YTMusicAPI/Auth/OAuth/Token.pm
Normal file
@@ -0,0 +1,61 @@
|
||||
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;
|
||||
Reference in New Issue
Block a user