google-tokens

Easily get your Google OAuth tokens

Downloads
198
Stars
4

Google-tokens

Tool for getting Google Cloud API OAuth tokens (refresh token)

Why?

Getting started

  1. Sign-up for a Google Cloud API account and create an API project.
  2. Create credentials (OAuth client id) and add a 'Authorized redirect URI' of http://localhost:3000/callback.
  3. Obtain your client_id & client_secret for usage in next step
    Also...
    See detailed instructions on how to create credentials

Steps to generate .gtokens.json

  1. git clone https://github.com/bradoyler/google-tokens.git
  2. copy .gtokens.example.json to .gtokens.json
  3. fill in client_id and client_secret
  4. install & run
npm install
npm start
  1. follow prompt and copy refresh_token to your '.gtokens.json'

API

const GoogleTokens = require('google-tokens')
const tokens = new GoogleTokens({ client_id, client_secret }) // OAuth credentials 

const scope = [
  'https://www.googleapis.com/auth/drive',
  'https://www.googleapis.com/auth/drive.readonly'
]

tokens.authPrompt({ scope }) // required to pass API scope
  .then(tokens => console.log('OAuthTokens:', tokens))
  .catch(console.error)

NOTE: you will only get a refresh_token the first-time you authenticate, but you can force to get new tokens by using the 'prompt' option, like so:

tokens.authPrompt({ scope, prompt: 'consent' })
  .then(tokens => console.log('OAuthTokens:', tokens))

Or you can run:

npm run refresh

TODOs:

  • add CLI support
  • allow for using shortnames (ie. auth/drive ) in scope
  • tests for all options