Google-RestApi
view release on metacpan or search on metacpan
bin/google_restapi_oauth_token_creator view on Meta::CPAN
=item * Gmail API
=item * Tasks API
=back
You only need to enable the APIs you actually plan to use. Each failing scope
check test will print the exact Cloud Console URL to enable that specific API.
=back
=head2 3. Configure the OAuth Consent Screen
=over
=item * Go to B<APIs & Services E<gt> OAuth consent screen>.
=item * Choose B<External> and click B<Create>.
=item * Fill in the required fields (app name, support email) and click B<Save and Continue>
through the remaining steps.
=item * On the B<Test users> page, add the Google account(s) you will use for testing.
Only listed users can authorise your app while it remains in Testing mode.
=back
=head2 4. Create OAuth Credentials
=over
=item * Go to B<APIs & Services E<gt> Credentials>.
=item * Click B<+ Create Credentials> and select B<OAuth client ID>.
=item * Choose B<Desktop app>, give it a name, and click B<Create>.
=item * Copy the B<Client ID> and B<Client Secret> shown in the confirmation dialog.
=back
=head2 5. Create a Config File
Create a YAML file with your credentials. If the config file is shared with
other applications, nest the Google::RestApi configuration under an optional
C<google_restapi> top-level key; otherwise place it at the root:
---
# shared config â google_restapi section is used by this package:
google_restapi:
auth:
class: OAuth2Client
client_id: <client-id-from-google>
client_secret: <client-secret-from-google>
token_file: <filename-for-the-stored-token> # bare filename (resolved against the config file's directory) or an absolute path
scope:
- https://www.googleapis.com/auth/drive
- https://www.googleapis.com/auth/spreadsheets
# add further scopes as needed:
# - https://www.googleapis.com/auth/calendar
# - https://www.googleapis.com/auth/documents
# - https://www.googleapis.com/auth/gmail.modify
# - https://www.googleapis.com/auth/tasks
other_app:
some_key: some_value
# or, without the google_restapi wrapper (root-level config):
---
auth:
class: OAuth2Client
client_id: <client-id-from-google>
client_secret: <client-secret-from-google>
token_file: <filename-for-the-stored-token>
scope:
- https://www.googleapis.com/auth/drive
- https://www.googleapis.com/auth/spreadsheets
The C<scope> list controls which Google APIs the token is authorised to access.
Only include the scopes for APIs you actually use â requesting unnecessary scopes
may cause Google to show a warning during authorisation. If omitted, a minimal
default scope is used (userinfo only).
If C<token_file> is a bare filename it is written to the same directory as the
config file; if it is an absolute path it is written there directly. The same
config file is used by this package at runtime to access the Google APIs.
=head2 6. Run This Script
perl bin/google_restapi_oauth_token_creator /path/to/your/config.yaml
Follow the prompts:
=over
=item * Copy the printed URL and paste it into a browser.
=item * Log in to your Google account if prompted, then click B<Allow>.
=item * Copy the authorisation code from the resulting page and paste it back
into the terminal.
=back
The script will store the token and then run a scope check to confirm that all
enabled APIs are reachable. Any failing check will print the Cloud Console URL
needed to enable that API.
=head2 7. Use the Token
use Google::RestApi;
my $rest_api = Google::RestApi->new(config_file => '/path/to/your/config.yaml');
See L<Google::RestApi> for further details.
=head1 RE-RUNNING THE SCOPE CHECK
If you enable additional APIs later and want to verify without re-creating the
token, run:
GOOGLE_RESTAPI_CONFIG=/path/to/your/config.yaml \
( run in 0.881 second using v1.01-cache-2.11-cpan-85f18b9d64f )