Google APIs Ruby Client (gem) Error `uninitialized constant



这确实让我困惑了几个小时。 我已经按照 Baugues 的详细情况引导了我的应用程序,以至于通过 OAuth2 进行身份验证有效,我只是在session#create(回调)操作中进行测试。 下面是一些代码:

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

身份验证后,上述逻辑在 callback 方法中执行 - 出于此粗略测试的目的,该方法呈现create.html.erb . 我已经注释掉了刚刚回显到视图中的@result实例变量。

但是,Google::APIClient::UploadIO.new("foo", "text/plain")显然不应该触发uninitialized constant Google::APIClient::UploadIO。 我已经挖掘了这颗宝石的来源,UploadIOrequired media.rb宝石。

感谢建议和帮助!

裁判:

  • 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

检查您的 Gemfile.lock 以查看它实际使用的 google-api-client 版本。当我完成相同的步骤时,看起来它默认使用 0.3.0,可能是由于 google-omniauth-plugin 的依赖性有点落后。0.3.0 中没有媒体支持。

尝试将宝石文件更改为

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

并重新运行"捆绑安装"以强制其使用较新版本。

对于偶然发现google-api-client gem 版本大于或等于 0.9 的人,您需要使用类似以下内容:

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

将"analytics_v3"与您正在使用的生成的谷歌服务 API 交换。

有关生成的 API 名称的完整列表,请参阅:https://github.com/google/google-api-ruby-client/tree/master/generated/google/apis

在这里您可以看到如何从版本 0.8.* 迁移到 0.9.*

在 0.8.x 中,库会动态"发现"API,从而引入额外的网络调用和不稳定。这已在 0.9 中修复。

要在 0.8.x 中获取驱动器客户端,需要这样做:

require 'google/api_client'
client = Google::APIClient.new
drive = client.discovered_api('drive', 'v2')

在 0.9 中,同样的事情可以像这样完成:

require 'google/apis/drive_v2'
drive = Google::Apis::DriveV2::DriveService.new

所有 API 都可以立即访问,而无需额外的网络调用或运行时代码生成。

接口方法

API 方法的调用样式已更改。在 0.8.x 中,所有调用都是通过通用执行方法进行的。在 0.9 中,生成的服务为所有可用方法完全定义了方法签名。

要使用 0.8.x 中的 Google 云端硬盘 API 获取文件,需要这样做:

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

在 0.9 中,同样的事情可以像这样完成:

file = drive.get_file('abc123')

完整的 API 定义,包括可用的方法、参数和数据类,可以在生成的目录中找到。

链接

最新更新