Update repository structure
Some checks failed
release / release-plugins (push) Failing after 19s

This commit is contained in:
mschuepbach
2024-03-28 21:44:32 +01:00
parent 54dfd10aa9
commit 6fdb7bc11c
8 changed files with 1 additions and 1 deletions

121
OAuth2.pm Normal file
View File

@@ -0,0 +1,121 @@
package Plugins::YouTubeMusic::OAuth2;
use strict;
use Slim::Utils::Log;
use Slim::Utils::Cache;
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_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 ) };
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 ) };
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;