Traceback (most recent call last):
5: from init.rb:42:in `<main>'
4: from init.rb:22:in `authorize'
3: from /home/perro/.rvm/gems/ruby-2.5.1/gems/googleauth-0.14.0/lib/googleauth/client_id.rb:82:in `from_file'
2: from /home/perro/.rvm/gems/ruby-2.5.1/gems/googleauth-0.14.0/lib/googleauth/client_id.rb:82:in `open'
1: from /home/perro/.rvm/gems/ruby-2.5.1/gems/googleauth-0.14.0/lib/googleauth/client_id.rb:85:in `block in from_file'
/home/perro/.rvm/gems/ruby-2.5.1/gems/googleauth-0.14.0/lib/googleauth/client_id.rb:99:in `from_hash': Expected top level property 'installed' or 'web' to be present. (RuntimeError)
{
"type": "service_account",
"project_id": "xxxx",
"private_key_id": "xxxx",
"private_key": "xxxx",
"client_email": "xxxx",
"client_id": "xxxx",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "xxxx"
}
require "google/apis/drive_v3"
require "googleauth"
require "googleauth/stores/file_token_store"
require "fileutils"
OOB_URI = "urn:ietf:wg:oauth:2.0:oob".freeze
APPLICATION_NAME = "Drive API Ruby Quickstart".freeze
CREDENTIALS_PATH = "auth.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::DriveV3::AUTH_DRIVE_METADATA_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
# Initialize the API
drive_service = Google::Apis::DriveV3::DriveService.new
drive_service.client_options.application_name = APPLICATION_NAME
drive_service.authorization = authorize
# List the 10 most recently modified files.
response = drive_service.list_files(page_size: 10,
fields: "nextPageToken, files(id, name)")
puts "Files:"
puts "No files found" if response.files.empty?
response.files.each do |file|
puts "#{file.name} (#{file.id})"
end