0

I can't understand how to access to sheets after OOB deprecation.

Here https://github.com/googleapis/google-cloud-ruby#authentication there is no example and the sample project, Quickstart, https://developers.google.com/sheets/api/quickstart/python is missing the ruby implementation.

I've already a credentials.json file but I don't see how to use it.

Can you point me in the right direction, please?

TIA

  • in theory the old installed application sample for ruby should still work. If you can find one that is. – Linda Lawton - DaImTo Aug 27 '23 at 14:49
  • Thanks @LindaLawton-DaImTo but it does not work because it uses OOB see: https://github.com/gimite/google-drive-ruby/blob/master/lib/google_drive/session.rb#L158 What puzzles me is how to authenticate my software – PuzzledDavid Aug 27 '23 at 21:18
  • 1
    are you aware that isn't the official drive project for ruby right this is third party. – Linda Lawton - DaImTo Aug 28 '23 at 07:53
  • try https://github.com/googleapis/google-api-ruby-client/blob/main/samples/cli/lib/samples/drive.rb – Linda Lawton - DaImTo Aug 28 '23 at 07:58
  • 1
    @LindaLawton-DaImTo right! I missed the project wasn't official, I'll try the code at your link and feedback you. Thank you very much – PuzzledDavid Aug 28 '23 at 10:49
  • Hi @LindaLawton-DaImTo I had no success trying the code you pointed me. It resolves the authorization in the method user_credentials_for here: https://github.com/googleapis/google-api-ruby-client/blob/main/samples/cli/lib/base_cli.rb#L64 That is at least outdated, in fact it uses OOB_URI if no credentials are provided (see line 79). Despite of, I have both the credentials.json and the token.yaml files but I need a user_id and I can't find where I can get it. Any hint? TIA – PuzzledDavid Aug 28 '23 at 16:14
  • Moreover @LindaLawton-DaImTo other official examples here: https://github.com/googleapis/google-api-ruby-client/tree/main are missing authorization details, deferring to vague: "See Googleauth or Signet libraries". Can you point me to some valid authorization code, please? it would matter a lot to me. TIA – PuzzledDavid Aug 28 '23 at 16:15
  • Check the auth library. https://github.com/googleapis/google-auth-library-ruby If you cant get it working let me know I can try but my ruby skills are really limited. And before you ask why its deprecated for console app I already posted this [446](https://github.com/googleapis/google-auth-library-ruby/issues/446) – Linda Lawton - DaImTo Aug 28 '23 at 16:24

1 Answers1

1

Okay so i was board. And wanted to prove that the library worked even though they deprecated cosole.

Pro tip when it authorizes the code you want is in the URL bar the page shows a 404 ignore that.

http://localhost/oauth2callback?code=4/0Adeu5BXVFsiiPPHivx2BW99wivd2gXcpEY1VOWbnCSM5o8pkvp8UBvmkH05u9SJh9gdynA&scope=email%20profile%20https://www.googleapis.com/auth/drive%20https://www.googleapis.com/auth/analytics.readonly%20https://www.googleapis.com/auth/drive.metadata.readonly%20openid%20https://www.googleapis.com/auth/userinfo.profile%20https://www.googleapis.com/auth/userinfo.email&authuser=0&prompt=consent

You should be copying the 4/0Adeu5BXVFsiiPPHivx2BW99wivd2gXcpEY1VOWbnCSM5o8pkvp8UBvmkH05u9SJh9gdynA part

The reason it shows a 404 error is because you are not running a local web server. So it cant find the page in question. But the response still comes back from Google with the code you need. This is kind of the draw back of the removal of oob.

require 'googleauth'
require "googleauth/stores/file_token_store"
require 'google/apis/drive_v3'

#  gem install google_auth
#  gem install google_drive

OOB_URI = 'http://localhost'
CREDENTIALS_FILE_PATH = 'C:\Development\FreeLance\GoogleSamples\Credentials\credentials.json'

scope = 'https://www.googleapis.com/auth/drive'
client_id = Google::Auth::ClientId.from_file(CREDENTIALS_FILE_PATH)
token_store = Google::Auth::Stores::FileTokenStore.new(:file => 'C:\Users\linda\RubymineProjects\tokens.yaml')
authorizer = Google::Auth::UserAuthorizer.new(client_id, scope, token_store)

user_id = ENV['USER']
credentials = authorizer.get_credentials(user_id)
if credentials.nil?
  url = authorizer.get_authorization_url(base_url: OOB_URI )
  puts "Open #{url} in your browser and enter the resulting code:"
  code = gets
  credentials = authorizer.get_and_store_credentials_from_code(
    user_id: user_id, code: code, base_url: OOB_URI)
end

drive = Google::Apis::DriveV3::DriveService.new
drive.authorization = credentials

# Search for files in Drive (first page only)
files = drive.list_files(q: "title contains 'finances'")
files.items.each do |file|
  puts file.title
end

# Upload a file
metadata = Google::Apis::DriveV3::File.new(name: 'test.txt')
metadata = drive.create_file(metadata, upload_source: '/tmp/test.txt', content_type: 'text/plain')

# Download a file
drive.get_file(metadata.id, download_dest: '/tmp/downloaded-test.txt')

# OK to use credentials
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Thanks for the code and your patience, but unfortunately when I put the url the code suggests, i.e. something like: "Open https://accounts.google.com/o/oauth2/auth?access_type=offline&...... in your browser and enter the resulting code:" I ignore the error, as you said, if I understand your: "when it authorizes the code you want is in the URL bard the page shows a 404 ignore that." But then the code returns: `Authorization failed. Server message: (Signet::AuthorizationError) { "error": "invalid_grant", "error_description": "Malformed auth code." }` :-( – PuzzledDavid Aug 28 '23 at 18:53
  • Try and copy it again. It works fine. you just want the code part of the url bar. copy that and paste it into the app and it will authorize it for you. – Linda Lawton - DaImTo Aug 28 '23 at 19:31
  • Found it!! I should put the value of the **"code" parameter** that appears as response. For other people, the response should be something like: `Not Found /oauth2callback?scope=https://www.googleapis.com/auth/drive&code=XXXXXXXXXXXXXXXX` – PuzzledDavid Aug 28 '23 at 20:01