This commit is contained in:
@@ -8,93 +8,114 @@ use Slim::Utils::Strings qw(string cstring);
|
||||
use JSON::XS::VersionOneAndTwo;
|
||||
use Data::Dumper;
|
||||
|
||||
use constant CLIENT_ID => "861556708454-d6dlm3lh05idd8npek18k6be8ba3oc68.apps.googleusercontent.com";
|
||||
use constant CLIENT_ID =>
|
||||
"861556708454-d6dlm3lh05idd8npek18k6be8ba3oc68.apps.googleusercontent.com";
|
||||
use constant CLIENT_SECRET => "SboVhoG9s0rNafixCSGGKXAT";
|
||||
|
||||
my $log = logger('plugin.youtubemusic');
|
||||
my $cache = Slim::Utils::Cache->new();
|
||||
|
||||
sub getDeviceCode {
|
||||
my $post = "client_id=" . CLIENT_ID . "&scope=https://www.googleapis.com/auth/youtube";
|
||||
|
||||
my $http = Slim::Networking::SimpleAsyncHTTP->new(
|
||||
sub {
|
||||
my $response = shift;
|
||||
my $result = eval { from_json($response->content) };
|
||||
my $post =
|
||||
"client_id="
|
||||
. CLIENT_ID
|
||||
. "&scope=https://www.googleapis.com/auth/youtube";
|
||||
|
||||
if ($@) {
|
||||
$log->error(Data::Dump::dump($response)) unless main::DEBUGLOG && $log->is_debug;
|
||||
$log->error($@);
|
||||
} else {
|
||||
$cache->set("yt:device_code", $result->{device_code}, $result->{expires_in});
|
||||
$cache->set("yt:verification_url", $result->{verification_url}, $result->{expires_in});
|
||||
$cache->set("yt:user_code", $result->{user_code}, $result->{expires_in});
|
||||
my $http = Slim::Networking::SimpleAsyncHTTP->new(
|
||||
sub {
|
||||
my $response = shift;
|
||||
my $result = eval { from_json( $response->content ) };
|
||||
|
||||
$log->debug("content:", $response->content);
|
||||
}
|
||||
},
|
||||
sub {
|
||||
$log->error($_[1]);
|
||||
},
|
||||
{
|
||||
timeout => 15,
|
||||
}
|
||||
);
|
||||
|
||||
$http->post(
|
||||
"https://www.youtube.com/o/oauth2/device/code",
|
||||
'Content-Type' => 'application/x-www-form-urlencoded',
|
||||
$post,
|
||||
);
|
||||
if ($@) {
|
||||
$log->error( Data::Dump::dump($response) )
|
||||
unless main::DEBUGLOG && $log->is_debug;
|
||||
$log->error($@);
|
||||
}
|
||||
else {
|
||||
$cache->set( "yt:device_code", $result->{device_code},
|
||||
$result->{expires_in} );
|
||||
$cache->set( "yt:verification_url", $result->{verification_url},
|
||||
$result->{expires_in} );
|
||||
$cache->set( "yt:user_code", $result->{user_code},
|
||||
$result->{expires_in} );
|
||||
|
||||
$log->debug( "content:", $response->content );
|
||||
}
|
||||
},
|
||||
sub {
|
||||
$log->error( $_[1] );
|
||||
},
|
||||
{
|
||||
timeout => 15,
|
||||
}
|
||||
);
|
||||
|
||||
$http->post(
|
||||
"https://www.youtube.com/o/oauth2/device/code",
|
||||
'Content-Type' => 'application/x-www-form-urlencoded',
|
||||
$post,
|
||||
);
|
||||
}
|
||||
|
||||
sub getToken {
|
||||
my $cb = shift;
|
||||
my @params = @_;
|
||||
my $post = "client_id=" . CLIENT_ID . "&client_secret=" . CLIENT_SECRET;
|
||||
my $code = $cache->get('yt:device_code');
|
||||
|
||||
if (defined $code) {
|
||||
$post .= "&code=$code&grant_type=http://oauth.net/grant_type/device/1.0";
|
||||
} else {
|
||||
$post .= "&refresh_token=" . $cache->get('yt:refresh_token') . "&grant_type=refresh_token";
|
||||
}
|
||||
|
||||
my $http = Slim::Networking::SimpleAsyncHTTP->new(
|
||||
sub {
|
||||
my $response = shift;
|
||||
my $result = eval { from_json($response->content) };
|
||||
my $cb = shift;
|
||||
my @params = @_;
|
||||
my $post = "client_id=" . CLIENT_ID . "&client_secret=" . CLIENT_SECRET;
|
||||
my $code = $cache->get('yt:device_code');
|
||||
|
||||
if ($@) {
|
||||
$log->error(Data::Dump::dump($response)) unless main::DEBUGLOG && $log->is_debug;
|
||||
$log->error($@);
|
||||
} else {
|
||||
$cache->set("yt:access_token", $result->{access_token}, $result->{expires_in} - 60);
|
||||
$cache->set('yt:refresh_token', $result->{refresh_token}) if $result->{refresh_token};
|
||||
|
||||
$cache->remove('yt:user_code');
|
||||
$cache->remove('yt:verification_url');
|
||||
$cache->remove('yt:device_code');
|
||||
|
||||
$log->debug("content:", $response->content);
|
||||
if ( defined $code ) {
|
||||
$post .=
|
||||
"&code=$code&grant_type=http://oauth.net/grant_type/device/1.0";
|
||||
}
|
||||
else {
|
||||
$post .=
|
||||
"&refresh_token="
|
||||
. $cache->get('yt:refresh_token')
|
||||
. "&grant_type=refresh_token";
|
||||
}
|
||||
|
||||
$cb->(@params) if $cb;
|
||||
}
|
||||
},
|
||||
sub {
|
||||
$log->error($_[1]);
|
||||
$cb->(@params) if $cb;
|
||||
},
|
||||
{
|
||||
timeout => 15,
|
||||
}
|
||||
);
|
||||
|
||||
$http->post(
|
||||
"https://oauth2.googleapis.com/token",
|
||||
'Content-Type' => 'application/x-www-form-urlencoded',
|
||||
$post,
|
||||
);
|
||||
my $http = Slim::Networking::SimpleAsyncHTTP->new(
|
||||
sub {
|
||||
my $response = shift;
|
||||
my $result = eval { from_json( $response->content ) };
|
||||
|
||||
if ($@) {
|
||||
$log->error( Data::Dump::dump($response) )
|
||||
unless main::DEBUGLOG && $log->is_debug;
|
||||
$log->error($@);
|
||||
}
|
||||
else {
|
||||
$cache->set(
|
||||
"yt:access_token",
|
||||
$result->{access_token},
|
||||
$result->{expires_in} - 60
|
||||
);
|
||||
$cache->set( 'yt:refresh_token', $result->{refresh_token} )
|
||||
if $result->{refresh_token};
|
||||
|
||||
$cache->remove('yt:user_code');
|
||||
$cache->remove('yt:verification_url');
|
||||
$cache->remove('yt:device_code');
|
||||
|
||||
$log->debug( "content:", $response->content );
|
||||
|
||||
$cb->(@params) if $cb;
|
||||
}
|
||||
},
|
||||
sub {
|
||||
$log->error( $_[1] );
|
||||
$cb->(@params) if $cb;
|
||||
},
|
||||
{
|
||||
timeout => 15,
|
||||
}
|
||||
);
|
||||
|
||||
$http->post(
|
||||
"https://oauth2.googleapis.com/token",
|
||||
'Content-Type' => 'application/x-www-form-urlencoded',
|
||||
$post,
|
||||
);
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
Reference in New Issue
Block a user