/ozb-google-oauth2-utils

Google OAuth2 utility

Primary LanguageScalaMIT LicenseMIT

ozb-google-oauth2-utils

Command-line utility to manage Google APIs authorization (OAuth2) tokens.
Useful to generate access/refresh tokens (for Installed Applications).

Features

  • generate access/refresh tokens

  • use a refresh token to generate a new access token

Requirements

This has been implemented in Scala, so you need a Java Runtime environment 1.7+.

Installing

I haven’t published any binary so far because it has been used for personal purposes only.
Now, I could push a zip file onto some cloud file storage service if kindly asked ;).

In the meantime, to run this tools, you need to build it and generate an executable script.

He are the main steps:

  1. clone this repo

  2. make sure you have Java 7+, Scala 2.11 and SBT (the last two can be installed very easily on OS X with Homebrew)

  3. simply run sbt stage and a directory with all the needed files will be generated under ./target/universal/stage (thanks SBT Native Packager !)

  4. then run the shell script under the bin/ directory

Usage

org.ozb.google.oauth2 0.1 : Google OAuth2 command-line utils
Usage: org.ozb.google.oauth2 [GetTokenResponse|RefreshAccessToken] [options]

  --help
        prints this usage text
  --clientId <value>
        clientId
  --clientSecret <value>
        clientSecret
  --clientSecretJson <value>
        clientSecret JSON file path
  --redirectUri <value>
        redirectUri (by default 'urn:ietf:wg:oauth:2.0:oob')
Command: GetTokenResponse [options]
	Get OAuth2 authorization token (access token and refresh token)
  --scopes <value>
        scopes (e.g."https://spreadsheets.google.com/feeds")
Command: RefreshAccessToken [options]
	Refresh access token
  --refreshToken <value>

Upon successful invocation, the token response will be printed out at the console.
The refresh token should be stored in long-term storage to request a new access token when it’s no longer valid.

Client ID & secret

You can specify the clientId and the clientSecret in three ways:

  1. either by passing them in the command line

    --clientId xxxx --clientSecret yyyy ...
  2. or by specifying the clientSecrets.json file (downloaded from the Google Developers Console)

    --clientSecretJson path/to/client_secret.json ...
  3. or finally, by entering them directly in the terminal if no clientId/clientSecret/clientSecretJson option is passed in the command-line

Examples

Generate an access and refresh token for the spreadsheets API
ozb-google-oauth2-utils GetTokenResponse --clientSecretJson /path/to/yours/client_secret.json --scopes https://spreadsheets.google.com/feeds
Refresh the access token
ozb-google-oauth2-utils RefreshAccessToken --clientSecretJson /path/to/yours/client_secret.json

In this case, the refresh token will be asked at the console (but you can pass it on the command-line)