Get device code for authentication
All checks were successful
release / release-plugins (push) Successful in 25s

This commit is contained in:
mschuepbach
2024-03-16 11:27:17 +01:00
parent ab83ff0261
commit 2129228491
4 changed files with 75 additions and 0 deletions

View File

@@ -6,4 +6,12 @@
<input type="text" class="stdedit" name="pref_testPref" id="pref_testPref" value="[% prefs.pref_testPref %]" size="45"> <input type="text" class="stdedit" name="pref_testPref" id="pref_testPref" value="[% prefs.pref_testPref %]" size="45">
[% END %] [% END %]
[% WRAPPER setting title="PLUGIN_YOUTUBEMUSIC_AUTH" desc="PLUGIN_YOUTUBEMUSIC_AUTH_DESC" %]
<input type="submit" name="get_device_code" value=[% "PLUGIN_YOUTUBEMUSIC_START_AUTH" | string %]
[% IF user_code %]
<a href="[% verification_url %]" target="none">[% "PLUGIN_YOUTUBEMUSIC_VERIFICATIONURL" | string %]</a>
<div>[% user_code %]</div>
[% END %]
[% END %]
[% PROCESS settings/footer.html %] [% PROCESS settings/footer.html %]

49
YouTubeMusic/OAuth2.pm Normal file
View File

@@ -0,0 +1,49 @@
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;
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 $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,
);
}
1;

View File

@@ -7,6 +7,7 @@ use Slim::Utils::Log;
use Slim::Utils::Prefs; use Slim::Utils::Prefs;
my $log = logger('plugin.youtubemusic'); my $log = logger('plugin.youtubemusic');
my $cache = Slim::Utils::Cache->new();
sub name { sub name {
return 'PLUGIN_YOUTUBEMUSIC'; return 'PLUGIN_YOUTUBEMUSIC';
@@ -27,6 +28,11 @@ sub handler {
preferences('plugin.youtubemusic')->set('testPref', $params->{'pref_testPref'}); preferences('plugin.youtubemusic')->set('testPref', $params->{'pref_testPref'});
} }
Plugins::YouTubeMusic::OAuth2::getDeviceCode if $params->{get_device_code};
$params->{user_code} = $cache->get('yt:user_code');
$params->{verification_url} = $cache->get('yt:verification_url');
return $class->SUPER::handler($client, $params); return $class->SUPER::handler($client, $params);
} }

View File

@@ -10,3 +10,15 @@ PLUGIN_YOUTUBEMUSIC_TESTPREF
PLUGIN_YOUTUBEMUSIC_TESTPREF_DESC PLUGIN_YOUTUBEMUSIC_TESTPREF_DESC
EN This is a test. EN This is a test.
PLUGIN_YOUTUBEMUSIC_AUTH
EN Login with Google
PLUGIN_YOUTUBEMUSIC_AUTH_DESC
EN You need to login with your Google account to use this plugin.
PLUGIN_YOUTUBEMUSIC_START_AUTH
EN Start
PLUGIN_YOUTUBEMUSIC_VERIFICATIONURL
EN Login