4

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:

Michael De Silva
  • 3,808
  • 1
  • 20
  • 24

3 Answers3

8

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.

Steve Bazyl
  • 11,002
  • 3
  • 21
  • 24
3

For people who stumble across this whose google-api-client gem version is greater than or equal to 0.9 you will want to use something like:

gem 'google-api-client', :require => 'google/apis/analytics_v3'

Exchanging "analytics_v3" with the generated google service api that you are using.

For a complete list of generated API names, see: https://github.com/google/google-api-ruby-client/tree/master/generated/google/apis

Joe Edgar
  • 872
  • 5
  • 13
3

Here you can see how to migrate from version 0.8.* to 0.9.*

In 0.8.x the library would "discover" APIs on the fly, introducing additional network calls and instability. That has been fixed in 0.9.

To get the drive client in 0.8.x required this:

require 'google/api_client'

client = Google::APIClient.new
drive = client.discovered_api('drive', 'v2')

In 0.9 the same thing can be accomplished like this:

require 'google/apis/drive_v2'

drive = Google::Apis::DriveV2::DriveService.new

All APIs are immediately accessible without requiring additional network calls or runtime code generation.

API methods

The calling style for API methods has changed. In 0.8.x all calls were via a generic execute method. In 0.9 the generated services have fully defined method signatures for all available methods.

To get a file using the Google Drive API in 0.8.x required this:

file = client.execute(:api_method => drive.file.get, :parameters => { 'id' => 'abc123' })

In 0.9 the same thing can be accomplished like this:

file = drive.get_file('abc123')

Full API definitions including available methods, parameters, and data classes can be found in the generated directory.

LINK

artamonovdev
  • 2,260
  • 1
  • 29
  • 33