Skip to content

Commit dd18738

Browse files
committed
Version 0.12 Release
#4 drill perltidy and POD Cleanup
1 parent a1e3ce5 commit dd18738

22 files changed

+1291
-1280
lines changed

README.txt

Lines changed: 30 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,47 @@ NAME
44

55
VERSION
66

7-
version 0.11
7+
version 0.12
88

99
SYNOPSIS
1010

1111
Access Google API Services Version 1 using an OAUTH2 User Agent
1212

13-
## use goauth CLI helper to create the OATH credentials file ( see perldoc goauth )
14-
1513
use WebService::GoogleAPI::Client;
16-
use Data::Dumper;
1714

18-
my $gapi = WebService::GoogleAPI::Client->new(debug => 0); # my $gapi = WebService::GoogleAPI::Client->new(access_token => '');
19-
my $user = '[email protected]'; # full gmail or G-Suite email
15+
## assumes gapi.json configuration in working directory with scoped project and user authorization
16+
17+
my $gapi_client = WebService::GoogleAPI::Client->new( debug => 1, gapi_json => './gapi.json', user=> '[email protected]' );
18+
19+
AUTOMATIC API REQUEST CONSTRUCTION - SEND EMAL
20+
21+
## using dotted API Endpoint id to invoke helper validation and default value interpolations etc to send email to self
22+
use Email::Simple; ## RFC2822 formatted messages
23+
use MIME::Base64;
24+
my $my_email_address = '[email protected]'
25+
2026

21-
$gapi->auth_storage->setup({type => 'jsonfile', path => './gapi.json' }); # by default
27+
my $raw_email_payload = encode_base64( Email::Simple->create( header => [To => $my_email_address,
28+
From => $my_email_address,
29+
Subject =>"Test email from '$my_email_address' ",],
30+
body => "This is the body of email to '$my_email_address'",
31+
)->as_string
32+
);
2233

23-
$gapi->user($user); ## allows to select user configuration by google auth'd email address
24-
$gapi->do_autorefresh(1); ## refresh auth token with refresh token if auth session has expired
34+
$gapi_client->api_query(
35+
api_endpoint_id => 'gmail.users.messages.send',
36+
options => { raw => $raw_email_payload },
37+
);
2538

26-
OAUTH CREDENTIALS FILE TO ACCESS SERVCICES
39+
MANUAL API REQUEST CONSTRUCTION - GET CALENDAR LIST
2740

28-
TODO
41+
## Completely manually constructed API End-Point Request to obtain Perl Data Structure converted from JSON response.
42+
my $res = $gapi_client->api_query(
43+
method => 'get',
44+
path => 'https://www.googleapis.com/calendar/users/me/calendarList',
45+
)->json;
2946

30-
BUILD
47+
new
3148

3249
WebService::GoogleAPI::Client->new( user => '[email protected]',
3350
gapi_json => '/fullpath/gapi.json' );
@@ -125,30 +142,7 @@ SYNOPSIS
125142

126143
DELEGATED FROM WebService::GoogleAPI::Client::Discovery
127144

128-
FUNCTIONAL CLASS PROPERTIES
129-
130-
ua
131-
132-
Is a WebService::GoogleAPI::Client::UserAgent
133-
134-
DELEGATE METHOD
135-
136-
METHODS DELEGATED TO PROPERTY CLASS INSTANCES
137-
138-
access_token ua handles: access_token auth_storage do_autorefresh
139-
get_scopes_as_array user
140-
141-
discovery handles: discover_all
142-
extract_method_discovery_detail_from_api_spec
143-
get_api_discovery_for_api_id get_method_meta
144-
145-
KEY FEATURES
146-
147-
Aim is to streamline endpoint access without forcing an additional
148-
opinionated abstraction layer that imposes unecessary congnitive load.
149-
Users of the module should be able to access all Google Services and
150-
either use helper features or fully construct requests in a way that is
151-
as portable to alternative approaches as possible.
145+
FEATURES
152146

153147
API Discovery with local caching using CHI File
154148

@@ -166,36 +160,6 @@ KEY FEATURES
166160
configuration, sccoping, authorization and obtaining access_ and
167161
refresh_ tokensn from users
168162

169-
TODO:
170-
171-
Refactor AuthStorage and Credentials modules - include capability to
172-
handle service accounts credentials which are not currently supported.
173-
See https://gist.github.com/gsainio/6322375
174-
175-
standardise terminology in documentation and code
176-
177-
include POD for delegated methods
178-
179-
basic CLI to query API Discovery data ( Potentially with OPEN/Swagger
180-
Output option ) potenitlaly evolving to something similar to gcloud
181-
182-
SEE ALSO
183-
184-
* "/cloud.google.com/apis/docs/cloud-client-libraries" in Google
185-
Cloud Client Libraries https:
186-
187-
* "/www.blog.google/products/google-cloud/" in Google Cloud Blog
188-
https:
189-
190-
* Moo::Google - The original code base later forked into
191-
WebService::Google::Client by Steve Dondley. Some shadows of the
192-
original design remain
193-
194-
* "/github.com/APIs-guru/google-discovery-to-swagger" in Google
195-
Swagger API https:
196-
197-
* OAuth2::Client::Google - Perl 6 OAuth2 Client
198-
199163
AUTHOR
200164

201165
Peter Scott <[email protected]>

bin/goauth

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#!perl
22

3-
use strict; use warnings; ## required because I can't work out how to get percritic to use my modern config
3+
use strict;
4+
use warnings; ## required because I can't work out how to get percritic to use my modern config
5+
46
package goauth;
57

6-
# ABSTRACT: CLI tool with mini http server for negotiating Google OAuth2 Authorisation access tokens that allow offline access to Google API Services on behalf of the user.
8+
# ABSTRACT: CLI tool with mini http server for negotiating Google OAuth2 Authorisation access tokens that allow offline access to Google API Services on behalf of the user.
79

810
=pod
911
@@ -90,9 +92,9 @@ else
9092
if ( -e $filename )
9193
{
9294
say "File $filename exists";
93-
input_if_not_exists( ['gapi/client_id', 'gapi/client_secret', 'gapi/scopes'] ); ## this potentially allows mreging with a json file with data external
94-
## to the app or to augment missing scope from file generated from
95-
## earlier versions of goauth from other libs
95+
input_if_not_exists( ['gapi/client_id', 'gapi/client_secret', 'gapi/scopes'] ); ## this potentially allows mreging with a json file with data external
96+
## to the app or to augment missing scope from file generated from
97+
## earlier versions of goauth from other libs
9698
runserver();
9799
}
98100
else
@@ -114,7 +116,8 @@ sub setup
114116

115117
$oauth->{ client_secret } = _stdin() || croak( 'client secret is required and has no default' );
116118

117-
print 'scopes ( space sep list - see https://developers.google.com/identity/protocols/googlescopes ): default is - email profile https://www.googleapis.com/auth/plus.profile.emails.read '
119+
print
120+
'scopes ( space sep list - see https://developers.google.com/identity/protocols/googlescopes ): default is - email profile https://www.googleapis.com/auth/plus.profile.emails.read '
118121
. "https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/contacts.readonly https://mail.google.com\n";
119122

120123
$oauth->{ scopes } = _stdin(); ## no croak because empty string is allowed an will evoke defaults
@@ -175,7 +178,7 @@ sub runserver
175178
key => $config->get( 'gapi/client_id' ), # $config->{gapi}{client_id},
176179
secret => $config->get( 'gapi/client_secret' ), #$config->{gapi}{client_secret},
177180
authorize_url => 'https://accounts.google.com/o/oauth2/v2/auth?response_type=code',
178-
token_url => 'https://www.googleapis.com/oauth2/v4/token' ## NB Google credentials.json specifies "https://www.googleapis.com/oauth2/v3/token"
181+
token_url => 'https://www.googleapis.com/oauth2/v4/token' ## NB Google credentials.json specifies "https://www.googleapis.com/oauth2/v3/token"
179182
}
180183
};
181184

@@ -205,14 +208,14 @@ sub runserver
205208
my $tokens;
206209
app->log->info( "Will store tokens in " . $config->getFilename( $config->pathToFile ) );
207210

208-
if ( $c->param( 'code' ) ) ## postback from google
211+
if ( $c->param( 'code' ) ) ## postback from google
209212
{
210213
app->log->info( "Authorization code was retrieved: " . $c->param( 'code' ) );
211214
$tokens = $c->get_new_tokens( $c->param( 'code' ) );
212215
app->log->info( "App got new tokens: " . Dumper $tokens);
213216
if ( $tokens )
214217
{
215-
218+
216219
if ( $tokens->{ id_token } )
217220
{
218221
# my $jwt = Mojo::JWT->new(claims => $tokens->{id_token});
@@ -223,10 +226,10 @@ sub runserver
223226
# carp "Decode header :".Dumper $header;
224227

225228
$user_data = decode_jwt( token => $tokens->{ id_token }, kid_keys => $c->ua->get( 'https://www.googleapis.com/oauth2/v3/certs' )->res->json, );
229+
226230
#carp "Decoded user data:" . Dumper $user_data;
227231
}
228232

229-
230233

231234
#$user_data->{email};
232235
#$user_data->{family_name}
@@ -238,22 +241,23 @@ sub runserver
238241
{
239242
$config->addToHash( 'gapi/tokens/' . $user_data->{ email }, 'refresh_token', $tokens->{ refresh_token } );
240243
}
241-
else ## with access_type=offline set we should receive a refresh token unless user already has an active one.
244+
else ## with access_type=offline set we should receive a refresh token unless user already has an active one.
242245
{
243246
## carp('Google JWT Did not incude a refresh token - when the access token expires services will become inaccessible');
244247
}
245248
}
249+
246250
#$c->render( json => $config->get( 'gapi' ) );
247251
$c->{ access_token } = $tokens->{ access_token };
248-
$c->{user_email} = $user_data->{email};
249-
$c->render( template => 'oauth_granted');
252+
$c->{ user_email } = $user_data->{ email };
253+
$c->render( template => 'oauth_granted' );
250254
}
251-
else ## PRESENT USER DEFAULT PAGE TO REQUEST GOOGLE AUTH'D ACCESS TO SERVICES
255+
else ## PRESENT USER DEFAULT PAGE TO REQUEST GOOGLE AUTH'D ACCESS TO SERVICES
252256
{
253257
$c->render( template => 'oauth' );
254258
}
255259
};
256-
app->secrets( ['putyourownsecretcookieseedhereforsecurity' . time] ); ## NB persistence cookies not required beyond server run
260+
app->secrets( ['putyourownsecretcookieseedhereforsecurity' . time] ); ## NB persistence cookies not required beyond server run
257261
app->start( 'daemon', '-l', "http://*:$port" );
258262
return 1;
259263
}
@@ -275,7 +279,6 @@ sub _stdin
275279
}
276280

277281

278-
279282
=head2 TODO: Improve user interface of the HTML templates beneath DATA section
280283
281284
=over 1

dist.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ author = Peter Scott <[email protected]>
33
license = Apache_2_0
44
copyright_holder = Peter Scott and others
55
copyright_year = 2017-2018
6-
version = 0.11
6+
version = 0.12
77
main_module = lib/WebService/GoogleAPI/Client.pm
88

99
[PruneCruft]

0 commit comments

Comments
 (0)