Search code examples
google-sheetsgoogle-api-ruby-client

Ruby authentication to access Google sheets after OOB ban


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


Solution

  • 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