Get access token for authentication
All checks were successful
release / release-plugins (push) Successful in 24s
All checks were successful
release / release-plugins (push) Successful in 24s
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user