Skip to content

Instantly share code, notes, and snippets.

@Xliff
Created January 30, 2020 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xliff/303485c7227990f8fe456ea964c8a80f to your computer and use it in GitHub Desktop.
Save Xliff/303485c7227990f8fe456ea964c8a80f to your computer and use it in GitHub Desktop.
Perl5 and Inline::Perl5

This script works in Perl5:

use Data::Dumper;
use JSON::MaybeXS;
use Net::Google::DataAPI::Auth::OAuth2;
use Net::Google::Spreadsheets;
use Net::OAuth2::AccessToken;

my $oauth2 = Net::Google::DataAPI::Auth::OAuth2->new({
  client_id     => 'ID.apps.googleusercontent.com',
  client_secret => 'MYSECRET',
  scope         => ['https://www.googleapis.com/auth/spreadsheets']
});

my $url = $oauth2.authorize_url;

my $file = 'tokenData.json';
open my $fh, '<', $file or die;
$/ = undef;
my $data = <$fh>;
close $fh;

my $J = JSON->new->allow_nonref;
my $json_object = decode_json($data);

my $t = Net::OAuth2::AccessToken->session_thaw($json_object,
  auto_refresh => 1,
  profile      => $oauth2->oauth2_webserver,
);
$oauth2->access_token($t);

my $service = Net::Google::Spreadsheets->new({ auth => $oauth2 });

my $spreadsheet = $service->spreadsheet({
  title => 'Website Development Request Form'
});

my $worksheet = $spreadsheet->worksheet({ title => 'Sheet1' });

for ($worksheet->rows) {
  print Dumper($_->content);
}

However this equivalent Raku script (or at least I think it's equivalent) does not work:

use JSON::Fast;
use Net::Google::DataAPI::Auth::OAuth2:from<Perl5>;
use Net::Google::Spreadsheets:from<Perl5>;
use Net::OAuth2::AccessToken:from<Perl5>;

my $oauth2 = Net::Google::DataAPI::Auth::OAuth2.new({
  client_id     => 'ID.apps.googleusercontent.com',
  client_secret => 'MYSECRET',
  scope         => ['https://www.googleapis.com/auth/spreadsheets']
});

my $url = $oauth2.authorize_url;

say "OAuth URL code is: { $url }";

my $t = Net::OAuth2::AccessToken.session_thaw(
  from-json( "tokenData.json".IO.slurp ),
  auto_refresh => 1,
  profile      => $oauth2.oauth2_webserver,
);
$oauth2->access_token($t);

my $service = Net::Google::Spreadsheets.new({ auth => $oauth2 });

my $spreadsheet = $service.spreadsheet({
  title => 'Website Development Request Form'
});

for @( $spreadsheet.worksheets ) {
  .say
}

my $worksheet = $spreadsheet.worksheet({ title => 'Sheet1' });

for $worksheet.rows {
  .gist.say;
}

The error I get from the Raku script?

Can't use string ("title") as a HASH ref while "strict refs" in use at /usr/local/share/perl/5.26.1/Net/Google/DataAPI/Role/Service.pm line 144.

  in method invoke-gv-args at /home/ubuntu/rakudobrew/versions/moar-blead/install/share/perl6/site/sources/46A21D2AC9383D74F5E3CE760D84D29F9009C6B8 (Inline::Perl5) line 727
  in method FALLBACK at /home/ubuntu/rakudobrew/versions/moar-blead/install/share/perl6/site/sources/30ACC62F7600A7721C2CCD4FE7BCE01AD10AFEE8 (Inline::Perl5::Object) line 66
  in block <unit> at grab-google-sheet.pl6 line 56

Any thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment