Skip to content

Commit

Permalink
- Added support for changing the default API key and the client ID an…
Browse files Browse the repository at this point in the history
…d client secret values. (#285)

Changing these values can be done by modifying the "~/.config/youtube-viewer/api.json" file. See the README.md file for more details.

This should allow logging in.

Thanks to @apetresc for suggesting this.
  • Loading branch information
trizen committed Feb 12, 2020
1 parent b50382c commit 3292f49
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 42 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ To package this application, run the following commands:
./Build install --install_path script=/usr/bin
```

### LOGGING IN

Starting with version 3.7.4, youtube-viewer provides the `~/.config/youtube-viewer/api.json` file, which allows changing the default API key and the client ID/SECRET values:

```json
{
"key": "API_KEY",
"client_id": "CLIENT_ID",
"client_secret": "CLIENT_SECRET"
}
```

* Replace `API_KEY` with your YouTube API key. Create a new key [here](https://console.developers.google.com/apis/dashboard).
* Replace `CLIENT_ID` and `CLIENT_SECRET` with your native client ID and client secret values, by creating a new OAuth 2.0 Client ID [here](https://console.developers.google.com/apis/api/youtube.googleapis.com/credentials).

See also: https://github.com/trizen/youtube-viewer/issues/285

### REVIEWS

* [EN] YOUTUBE VIEWER: A COMPLETE YOUTUBE CLIENT FOR LINUX [UBUNTU PPA]
Expand Down
66 changes: 59 additions & 7 deletions bin/gtk2-youtube-viewer
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#-------------------------------------------------------
# GTK YouTube Viewer
# Created on: 12 September 2010
# Latest edit on: 10 February 2020
# Latest edit on: 12 February 2020
# https://github.com/trizen/youtube-viewer
#-------------------------------------------------------

Expand Down Expand Up @@ -88,6 +88,7 @@ my $config_file = catfile($config_dir, "gtk-youtube-viewer.conf");
my $youtube_users_file = catfile($config_dir, 'youtube_users.txt');
my $history_file = catfile($config_dir, 'history.txt');
my $authentication_file = catfile($config_dir, 'reg.dat');
my $api_file = catfile($config_dir, 'api.json');

# Create the configuration directory
foreach my $dir ($config_dir) {
Expand Down Expand Up @@ -751,19 +752,70 @@ my $yv_obj = WWW::YoutubeViewer->new(
authentication_file => $authentication_file,
);

{
$yv_obj->set_client_id('923751928481.apps.googleusercontent.com');
$yv_obj->set_client_secret("\26/Ae]3\b\6\x186a:*#0\32\t\f\n\27\17GC`" ^ substr($key, -24));
$yv_obj->set_redirect_uri('urn:ietf:wg:oauth:2.0:oob');
}

if (-f $api_file) {

open(my $fh, '<', $api_file) or die "[!] Can't open file <<$api_file>> for reading: $!\n";
my $content = do { local $/; <$fh> };
my $api = $yv_obj->parse_json_string($content);

if (ref($api) ne 'HASH') {
die "[!] Invalid format inside file 'api.json'.\n";
}

my $orig_key = $yv_obj->get_key;
my $orig_client_id = $yv_obj->get_client_id;
my $orig_client_secret = $yv_obj->get_client_secret;

my $key = $api->{key};
my $client_id = $api->{client_id};
my $client_secret = $api->{client_secret};

if (defined($key)) {
$yv_obj->set_key($key) // do {
warn "[!] Invalid key: $key\n" if $key ne 'API_KEY';
$yv_obj->set_key($orig_key);
};
}
if (defined($client_id)) {
$yv_obj->set_client_id($client_id) // do {
warn "[!] Invalid client_id: $client_id\n" if $client_id ne 'CLIENT_ID';
$yv_obj->set_client_id($orig_client_id);
};
}
if (defined($client_secret)) {
$yv_obj->set_client_secret($client_secret) // do {
warn "[!] Invalid client_secret: $client_secret\n" if $client_secret ne 'CLIENT_SECRET';
$yv_obj->set_client_secret($orig_client_secret);
};
}
}
else {
open(my $fh, '>', $api_file) or warn "[!] Can't create file <<$api_file>>: $!\n";
print $fh <<"EOT";
{
"key": "API_KEY",
"client_id": "CLIENT_ID",
"client_secret": "CLIENT_SECRET"
}
EOT
close $fh;
}

$yv_obj->load_authentication_tokens();

if (defined $yv_obj->get_access_token()) {
show_user_panel();
}
else {
$statusbar->push(1, 'Not logged in.');
}

{
$yv_obj->set_client_id('923751928481.apps.googleusercontent.com');
$yv_obj->set_client_secret("\26/Ae]3\b\6\x186a:*#0\32\t\f\n\27\17GC`" ^ substr($key, -24));
$yv_obj->set_redirect_uri('urn:ietf:wg:oauth:2.0:oob');
}

require WWW::YoutubeViewer::Utils;
my $yv_utils = WWW::YoutubeViewer::Utils->new(thousand_separator => $CONFIG{thousand_separator},
youtube_url_format => $CONFIG{youtube_video_url},);
Expand Down
66 changes: 59 additions & 7 deletions bin/gtk3-youtube-viewer
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#-------------------------------------------------------
# GTK YouTube Viewer
# Created on: 12 September 2010
# Latest edit on: 10 February 2020
# Latest edit on: 12 February 2020
# https://github.com/trizen/youtube-viewer
#-------------------------------------------------------

Expand Down Expand Up @@ -89,6 +89,7 @@ my $youtube_users_file = catfile($config_dir, 'youtube_users.txt');
my $history_file = catfile($config_dir, 'history.txt');
my $session_file = catfile($config_dir, 'session.dat');
my $authentication_file = catfile($config_dir, 'reg.dat');
my $api_file = catfile($config_dir, 'api.json');

# Create the configuration directory
foreach my $dir ($config_dir) {
Expand Down Expand Up @@ -814,19 +815,70 @@ my $yv_obj = WWW::YoutubeViewer->new(
authentication_file => $authentication_file,
);

{
$yv_obj->set_client_id('923751928481.apps.googleusercontent.com');
$yv_obj->set_client_secret("\26/Ae]3\b\6\x186a:*#0\32\t\f\n\27\17GC`" ^ substr($key, -24));
$yv_obj->set_redirect_uri('urn:ietf:wg:oauth:2.0:oob');
}

if (-f $api_file) {

open(my $fh, '<', $api_file) or die "[!] Can't open file <<$api_file>> for reading: $!\n";
my $content = do { local $/; <$fh> };
my $api = $yv_obj->parse_json_string($content);

if (ref($api) ne 'HASH') {
die "[!] Invalid format inside file 'api.json'.\n";
}

my $orig_key = $yv_obj->get_key;
my $orig_client_id = $yv_obj->get_client_id;
my $orig_client_secret = $yv_obj->get_client_secret;

my $key = $api->{key};
my $client_id = $api->{client_id};
my $client_secret = $api->{client_secret};

if (defined($key)) {
$yv_obj->set_key($key) // do {
warn "[!] Invalid key: $key\n" if $key ne 'API_KEY';
$yv_obj->set_key($orig_key);
};
}
if (defined($client_id)) {
$yv_obj->set_client_id($client_id) // do {
warn "[!] Invalid client_id: $client_id\n" if $client_id ne 'CLIENT_ID';
$yv_obj->set_client_id($orig_client_id);
};
}
if (defined($client_secret)) {
$yv_obj->set_client_secret($client_secret) // do {
warn "[!] Invalid client_secret: $client_secret\n" if $client_secret ne 'CLIENT_SECRET';
$yv_obj->set_client_secret($orig_client_secret);
};
}
}
else {
open(my $fh, '>', $api_file) or warn "[!] Can't create file <<$api_file>>: $!\n";
print $fh <<"EOT";
{
"key": "API_KEY",
"client_id": "CLIENT_ID",
"client_secret": "CLIENT_SECRET"
}
EOT
close $fh;
}

$yv_obj->load_authentication_tokens();

if (defined $yv_obj->get_access_token()) {
show_user_panel();
}
else {
$statusbar->push(1, 'Not logged in.');
}

{
$yv_obj->set_client_id('923751928481.apps.googleusercontent.com');
$yv_obj->set_client_secret("\26/Ae]3\b\6\x186a:*#0\32\t\f\n\27\17GC`" ^ substr($key, -24));
$yv_obj->set_redirect_uri('urn:ietf:wg:oauth:2.0:oob');
}

require WWW::YoutubeViewer::Utils;
my $yv_utils = WWW::YoutubeViewer::Utils->new(thousand_separator => $CONFIG{thousand_separator},
youtube_url_format => $CONFIG{youtube_video_url},);
Expand Down
54 changes: 53 additions & 1 deletion bin/youtube-viewer
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#-------------------------------------------------------
# youtube-viewer
# Created on: 02 June 2010
# Latest edit on: 10 February 2020
# Latest edit on: 12 February 2020
# https://github.com/trizen/youtube-viewer
#-------------------------------------------------------

Expand Down Expand Up @@ -142,6 +142,7 @@ my $config_file = catfile($config_dir, "$execname.conf");
my $authentication_file = catfile($config_dir, 'reg.dat');
my $history_file = catfile($config_dir, 'history.txt');
my $watched_file = catfile($config_dir, 'watched.txt');
my $api_file = catfile($config_dir, 'api.json');

if (not -d $config_dir) {
require File::Path;
Expand Down Expand Up @@ -609,6 +610,57 @@ my $yv_obj = WWW::YoutubeViewer->new(
$yv_obj->set_redirect_uri('urn:ietf:wg:oauth:2.0:oob');
}

if (-f $api_file) {

open(my $fh, '<', $api_file) or die "Can't open file <<$api_file>> for reading: $!\n";
my $content = do { local $/; <$fh> };
my $api = $yv_obj->parse_json_string($content);

if (ref($api) ne 'HASH') {
die "[!] Invalid format inside file 'api.json'.\n";
}

my $orig_key = $yv_obj->get_key;
my $orig_client_id = $yv_obj->get_client_id;
my $orig_client_secret = $yv_obj->get_client_secret;

my $key = $api->{key};
my $client_id = $api->{client_id};
my $client_secret = $api->{client_secret};

if (defined($key)) {
$yv_obj->set_key($key) // do {
warn "[!] Invalid key: $key\n" if $key ne 'API_KEY';
$yv_obj->set_key($orig_key);
};
}
if (defined($client_id)) {
$yv_obj->set_client_id($client_id) // do {
warn "[!] Invalid client_id: $client_id\n" if $client_id ne 'CLIENT_ID';
$yv_obj->set_client_id($orig_client_id);
};
}
if (defined($client_secret)) {
$yv_obj->set_client_secret($client_secret) // do {
warn "[!] Invalid client_secret: $client_secret\n" if $client_secret ne 'CLIENT_SECRET';
$yv_obj->set_client_secret($orig_client_secret);
};
}
}
else {
open(my $fh, '>', $api_file) or warn "[!] Can't create file <<$api_file>>: $!\n";
print $fh <<"EOT";
{
"key": "API_KEY",
"client_id": "CLIENT_ID",
"client_secret": "CLIENT_SECRET"
}
EOT
close $fh;
}

$yv_obj->load_authentication_tokens();

require WWW::YoutubeViewer::Utils;
my $yv_utils = WWW::YoutubeViewer::Utils->new(youtube_url_format => $opt{youtube_video_url},
thousand_separator => $opt{thousand_separator},);
Expand Down
18 changes: 8 additions & 10 deletions lib/WWW/YoutubeViewer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,9 @@ my %valid_options = (
comments_order => {valid => [qw(time relevance)], default => 'time'},
subscriptions_order => {valid => [qw(alphabetical relevance unread)], default => undef},

# Others
# Misc
debug => {valid => [0 .. 3], default => 0},
lwp_timeout => {valid => [qr/^\d+\z/], default => 1},
key => {valid => [qr/^.{5}/], default => undef},
config_dir => {valid => [qr/^./], default => q{.}},
cache_dir => {valid => [qr/^./], default => q{.}},

Expand All @@ -87,12 +86,13 @@ my %valid_options = (

use_invidious_api => {valid => [1, 0], default => 0},

# OAuth stuff
client_id => {valid => [qr/^.{5}/], default => undef},
client_secret => {valid => [qr/^.{5}/], default => undef},
redirect_uri => {valid => [qr/^.{5}/], default => undef},
access_token => {valid => [qr/^.{5}/], default => undef},
refresh_token => {valid => [qr/^.{5}/], default => undef},
# API/OAuth
key => {valid => [qr/^.{15}/], default => undef},
client_id => {valid => [qr/^.{15}/], default => undef},
client_secret => {valid => [qr/^.{15}/], default => undef},
redirect_uri => {valid => [qr/^.{15}/], default => undef},
access_token => {valid => [qr/^.{15}/], default => undef},
refresh_token => {valid => [qr/^.{15}/], default => undef},

authentication_file => {valid => [qr/^./], default => undef},

Expand Down Expand Up @@ -177,8 +177,6 @@ sub new {
}
}

$self->load_authentication_tokens();

foreach my $invalid_key (keys %opts) {
warn "Invalid key: '${invalid_key}'";
}
Expand Down
34 changes: 17 additions & 17 deletions lib/WWW/YoutubeViewer/Authentication.pm
Original file line number Diff line number Diff line change
Expand Up @@ -106,25 +106,25 @@ sub load_authentication_tokens {
return 1;
}

if (defined(my $file = $self->get_authentication_file) and defined(my $key = $self->get_key)) {
if (-f $file) {
local $/ = __AUTH_EOL__;
open my $fh, '<:raw', $file or return;

my @tokens;
foreach my $i (0 .. 1) {
chomp(my $token = <$fh>);
$token =~ /\S/ || last;
push @tokens, $self->decode_token($token);
}

$self->set_access_token($tokens[0]) // return;
$self->set_refresh_token($tokens[1]) // return;

close $fh;
return 1;
my $file = $self->get_authentication_file() // return;
my $key = $self->get_key() // return;

if (-f $file) {
local $/ = __AUTH_EOL__;
open my $fh, '<:raw', $file or return;

my @tokens;
foreach my $i (0 .. 1) {
chomp(my $token = <$fh>);
$token =~ /\S/ || last;
push @tokens, $self->decode_token($token);
}

$self->set_access_token($tokens[0]) // return;
$self->set_refresh_token($tokens[1]) // return;

close $fh;
return 1;
}

return;
Expand Down

0 comments on commit 3292f49

Please sign in to comment.