API-Google

 view release on metacpan or  search on metacpan

bin/goauth  view on Meta::CPAN

#!perl

package goauth;
$goauth::VERSION = '0.12';

# ABSTRACT: CLI tool for easily getting of Google API access tokens. Supports multiple users 


use Mojolicious::Commands;
use Data::Dumper;
use Config::JSON;
use Tie::File;
use feature 'say';
use Net::EmptyPort qw(empty_port);

my $filename;
if ($ARGV[0]) {
  $filename = $ARGV[0];
} else {
  $filename = 'config.json';
}

if (-e $filename) {
  say "File $filename exists";
  input_if_not_exists(['gapi/client_id', 'gapi/client_secret']);
  runserver();
} else {
  say "JSON file $filename with API tokens not found. Creating new file...";
  setup();
  runserver();
}

sub setup {
  my $oauth = {};
  say "Obtain app client_id and client_secret from http://console.developers.google.com/";
  print "client_id: ";
  chomp ($oauth->{client_id} = <STDIN>);
  print "client_secret: ";
  chomp ($oauth->{client_secret} = <STDIN>);
  my $tokensfile = Config::JSON->create($filename);
  $tokensfile->set('gapi/client_id', $oauth->{client_id});
  $tokensfile->set('gapi/client_secret', $oauth->{client_secret});
  say 'OAuth details was updated!';
  # Remove comment for Mojolicious::Plugin::JSONConfig compatibility
  tie my @array, 'Tie::File', $filename or die $!;
  shift @array;
  untie @array;
};

sub input_if_not_exists {
  my $fields = shift;
  my $config = Config::JSON->new($filename);
  for my $i (@$fields) {
    if (!defined $config->get($i) ) {
      print "$i: ";
      chomp (my $val = <STDIN>);
      $config->set($i, $val);
    }
  }
}

sub runserver {
  my $port = empty_port(3000);
  say "Starting web server. Before authorization don't forget to set redirect_uri to http://127.0.0.1:$port/";
  $ENV{'GOAUTH_TOKENSFILE'} = $filename;
  Mojolicious::Commands->start_app('API::Google::Server', 'daemon', '-l', 'http://*:'.$port);
}

__END__



( run in 0.652 second using v1.01-cache-2.11-cpan-39bf76dae61 )