Script to create an OAuth2 token that can be stored and used later to authorize our REST API access.
Based on code from https://gist.github.com/hexaddikt/6738162
To create a config file for use as a perl script:
1. Read over https://stackoverflow.com/questions/11485271/google-oauth-2-authorization-error-redirect-uri-mismatch It has a lot of useful background information. 2. Obtain a client_id and client_secret from your Google Developer's console page.
a. Go to https://console.cloud.google.com/apis/dashboard, sign in, and create a project. b. Go to the 'Credentials' tab on the left, and then '+ Create Credientials' at the top of the screen and select 'OAuth client ID' from the dropdown. c. Select 'Desktop app' (for a simple script) or 'Web application' (for a web site). The rest of these instructions pertain to the easier 'Desktop app'. It's best to get this more simple config working first, then move to more complex setups once you're familiar with the process. d. Pick a name for your script app and click on 'Create'. Copy the 'Client ID' and 'Client Secret' for the next step.
3. Create a yaml file with the following format:
--- auth: class: OAuth2Client client_id: <client-id-from-google> client_secret: <client-secret-from-google> token_file: <file-name-to-store-your-generated-token> # just file name, not path.
The token file will be stored in the same directory as the config file. Once this process is complete, this same config file will be used by this package to access the Google API. 4. Run this script with the first arg pointing to the yaml file above, and follow the directions on the screen, which will take you through the following steps:
a. Copy the URL printed out, and paste the URL in a browser to load the page. b. On the resulting page, click OK (possibly after being asked to log in to your Google account). c. You will be redirected to a page that provides a string that you should copy and paste back into the terminal window, so this script can exchange it for an access token from Google, and store the token. That will be the token for this package. 5. Once you have the YAML config file and token file created, you can now use them with this package:
use Google::RestApi; my $rest_api = new Google::RestApi->new(config_file => <path to config file>);
See Google::RestApi doc for further information on how to initialize this object with the token file.
To install Google::RestApi, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Google::RestApi
CPAN shell
perl -MCPAN -e shell install Google::RestApi
For more information on module installation, please visit the detailed CPAN module installation guide.