Skip to content

How to get credentials.json and token.yaml

aravind9349 edited this page Jan 17, 2019 · 5 revisions

Welcome to the google-client wiki!

To enable the directory API

go to Google API Ruby

  • Click on ENABLE THE DIRECTORY API button
  • Select + Create a new project.
  • Enter your Application name
  • Download the configuration file.
  • Move the downloaded file to your current directory and ensure it is named credentials.json.

Install the google client library

gem install google-api-client -v '~> 0.8'


To generate the token file

Create a new file generate_token.rb and copy the below code in it.

require 'google/apis/admin_directory_v1'
require 'googleauth'
require 'googleauth/stores/file_token_store'
require 'fileutils'

OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'.freeze
APPLICATION_NAME = 'Groups API Client'.freeze
CREDENTIALS_PATH = 'credentials.json'.freeze
# The file token.yaml stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
TOKEN_PATH = 'token.yaml'.freeze
SCOPE = Google::Apis::AdminDirectoryV1::AUTH_ADMIN_DIRECTORY_USER_READONLY

##
# Ensure valid credentials, either by restoring from the saved credentials
# files or intitiating an OAuth2 authorization. If authorization is required,
# the user's default browser will be launched to approve the request.
#
# @return [Google::Auth::UserRefreshCredentials] OAuth2 credentials
def authorize
  client_id = Google::Auth::ClientId.from_file(CREDENTIALS_PATH)
  token_store = Google::Auth::Stores::FileTokenStore.new(file: TOKEN_PATH)
  authorizer = Google::Auth::UserAuthorizer.new(client_id, SCOPE, token_store)
  user_id = 'default'
  credentials = authorizer.get_credentials(user_id)
  if credentials.nil?
    url = authorizer.get_authorization_url(base_url: OOB_URI)
    puts 'Open the following URL in the browser and enter the ' \
         "resulting code after authorization:\n" + url
    code = gets
    credentials = authorizer.get_and_store_credentials_from_code(
      user_id: user_id, code: code, base_url: OOB_URI
    )
  end
  credentials
end

service = Google::Apis::AdminDirectoryV1::DirectoryService.new
service.authorization = authorize


Place your credentials.json file in the same directory and run on your terminal

ruby generate_token.rb

  • It will attempt to open a new window or tab in your default browser. If this fails, copy the URL from the console and manually open it in your browser.

  • If you are not already logged into your Google account, you will be prompted to log in. If you are logged into multiple Google accounts, you will be asked to select one account to use for the authorization.

  • Click the Allow button.

  • Please copy that code, switch to your terminal and paste it there:

  • It will automatically generate new token.yaml file in your directory

  • Copy the credentials.json and token.yaml file path and pass it for the google client project.

  • Pass default as your customer_id in your project

Clone this wiki locally