Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google APIs Ruby Client (gem) Error `uninitialized constant Google::APIClient::UploadIO`

This has certainly had me baffled for a couple hours. I've bootstrapped my application as detailed by Baugues to the point that authentication via OAuth2 works and I'm just testing things out in the session#create (callback) action. Here's some code:

class SessionsController < ApplicationController
  def create
    @auth = request.env["omniauth.auth"]
    @token = @auth["credentials"]["token"]
    client = Google::APIClient.new
    client.authorization.access_token = @token
    service = client.discovered_api('drive', 'v1')

    file_content = Google::APIClient::UploadIO.new("foo", "text/plain")

    # @result = client.execute(
    #   :api_method => service.files.get,
    #   :parameters => { 'id' => 1 },
    #   :headers => {'Content-Type' => 'application/json'})
  end
end

Upon authenticating, the above logic is executed in the callback method - which for the purpose of this crude test renders out create.html.erb. I've commented out the @result instance variable that's just echoed out into the view.

However, Google::APIClient::UploadIO.new("foo", "text/plain") triggers uninitialized constant Google::APIClient::UploadIO when it clearly should not. I've dug through the source of this gem and the UploadIO class is required in media.rb of the gem.

Advice and assistance appreciated!

Ref:

  • http://code.google.com/p/google-api-ruby-client/
  • https://developers.google.com/drive/v1/reference/files/insert
  • https://developers.google.com/drive/examples/ruby#saving_new_files
like image 534
Michael De Silva Avatar asked Dec 09 '22 00:12

Michael De Silva


1 Answers

Check your Gemfile.lock to see which version of google-api-client it is actually using. When I went through the same steps, it looks like it settled on 0.3.0 by default, likely due to google-omniauth-plugin being a little behind with its dependency. 0.3.0 doesn't have media support in it.

Trying changing your Gemfile to

gem 'google-api-client', '~> 0.4.3', :require => 'google/api_client'

and rerun 'bundle install' to force it to use the more recent version.

like image 157
Steve Bazyl Avatar answered May 26 '23 01:05

Steve Bazyl