Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re^2: language translate

by Aldebaran (Deacon)
on Nov 11, 2018 at 02:13 UTC ( #1225554=note: print w/replies, xml ) Need Help??

in reply to Re: language translate
in thread language translate

Thank you localshop for this write-up and your work on this module. I've been nibbling and lurking at these threads, all the while building out what I can do with them. I went ahead and signed up for google cloud services. Now that I've done it, I'm glad for it and have little fear that google is gonna run away with my credit card anytime soon. They do give you $300 worth of credit and a year for a trial period and promise not to upgrade to paying without your say-so.

I built the sdk for debian. 5 commands: easy-peasy. The client is offered in 6 syntaxes, but perl is not one of them. For people who say "perl is dead," that might be proof, but I wonder if perl comes at it differently. Let me ask the question, short of igniting a flame war, is perl unfit for this task?

Does this module, WebService::GoogleAPI::Client, seek to brook that gap?

I wrote a bash driver for this script so that I could set environment variables before invocation. I get a single warning or error:

$ ./ >1.txt Use of uninitialized value in split at /usr/local/share/perl/5.26.1/We +bService/GoogleAPI/Client/AuthStorage/ line 82. $

I'll put output, perl script, and bash driver between readmore tags

GOOGLE_APPLICATION_CREDENTIALS=/home/bob/2.scripts/pages/trans1-d8f004 +0850a0.json /home/bob/2.scripts/pages -rw-rw-r-- 1 bob bob 2314 Nov 7 14:42 trans1-d8f0040850a0.json -----output----- json is /home/bob/2.scripts/pages/trans1-d8f0040850a0.json -------script------ #!/usr/bin/perl -w use 5.011; use WebService::GoogleAPI::Client; use Data::Dumper qw (Dumper); =head1 =head2 PRE-REQUISITES Setup a Google Project in the Google Console and add the Translate API + Library. You may need to enable billing to access Google Cloud Services. Setup an OAUTH Credential set and feed this into the CLI goauth included in WebService::GoogleAPI::Client and use the tool to authoris +e your user to access the project which will also create the local gapi. +json config. =over 2 =item =item =item assumes gapi.json configuration in working directory with scoped + project and user authorization =back =cut ## assumes gapi.json configuration in working directory with scoped pr +oject and user authorization my $json = $ENV{GOOGLE_APPLICATION_CREDENTIALS}; say "json is $json"; my $gapi_client = WebService::GoogleAPI::Client->new( gapi_json => $js +on, debug=>1 ); my $r = $gapi_client->api_query( api_endpoint_id => 'translate.transl +ations.translate', options => { q=>'this is some text to transla +te into russian', target => 'ru', format => 'text', } ); say Dumper $r->json; say $r->json->{data}{translations}[0]{translatedText}; -------driver------ #!/bin/bash export GOOGLE_APPLICATION_CREDENTIALS="/home/bob/2.scripts/pages/trans +1-d8f0040850a0.json" env |grep "GOOGLE" pwd ls -l *.json echo "-----output-----" ./ echo "-------script------" cat echo "-------driver------" cat

It would seem that the Oauth isn't correct. What fields should the .json file have? I think one can show the fields with redacted values and not lose his shirt:

{ "type": "service_account", "project_id": "trans1-221819", "private_key_id": ..., "private_key": "-----BEGIN PRIVATE KEY-----... "client_email": "", "client_id": "115338096797549417908", "auth_uri": "", "token_uri": "", "auth_provider_x509_cert_url": " +/certs", "client_x509_cert_url": " +a/x509/" }

I don't think I made many changes from your script except to print values and I took out one of the debug => 0. Looking for a precise definition of what "scoped" project means. Thank you for your comments and scripts,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1225554]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2021-05-19 00:02 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (186 votes). Check out past polls.