Get access token for authentication
All checks were successful
release / release-plugins (push) Successful in 24s

This commit is contained in:
mschuepbach
2024-03-16 13:02:06 +01:00
parent 7aacce24e6
commit 32463e762e
3 changed files with 59 additions and 2 deletions

View File

@@ -11,6 +11,10 @@
[% IF user_code %]
<a href="[% verification_url %]" target="none">[% "PLUGIN_YOUTUBEMUSIC_VERIFICATIONURL" | string %]</a>
<div>[% user_code %]</div>
<input type="submit" name="get_token" value="Check">
[% IF access_token %]
<div>Success</div>
[% END %]
[% END %]
[% END %]

View File

@@ -8,12 +8,14 @@ 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=861556708454-d6dlm3lh05idd8npek18k6be8ba3oc68.apps.googleusercontent.com" .
"&scope=https://www.googleapis.com/auth/youtube";
my $post = "client_id=" . CLIENT_ID . "&scope=https://www.googleapis.com/auth/youtube";
my $http = Slim::Networking::SimpleAsyncHTTP->new(
sub {
@@ -46,4 +48,53 @@ sub getDeviceCode {
);
}
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=" . $prefs->get('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);
$prefs->set('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;

View File

@@ -29,9 +29,11 @@ sub handler {
}
Plugins::YouTubeMusic::OAuth2::getDeviceCode if $params->{get_device_code};
Plugins::YouTubeMusic::OAuth2::getToken if $params->{get_token};
$params->{user_code} = $cache->get('yt:user_code');
$params->{verification_url} = $cache->get('yt:verification_url');
$params->{access_token} = $cache->get('yt:access_token');
return $class->SUPER::handler($client, $params);
}