ruby - 为什么在使用 google API 对 google Big Query 运行查询时出现 NoMethodError?

标签 ruby google-api google-bigquery

我正在尝试使用 Ruby API 针对 Google Big Query 运行查询。

这是我使用 Ruby 的第一个项目,我仍在学习该语言。

这也是我使用 Google API 的第一个项目。

环境:

  • Windows 7
  • Ruby 1.9
  • 法拉第 0.90
  • Goolge API - 服务帐户身份验证

我的代码运行时不会通过以下方式发出任何警告或错误消息:

@client.authorization.fetch_access_token! 
doc = File.read('bigQueryAPI.json')
@bigQuery = @client.register_discovery_document('bigquery', 'v2', doc)

注意: @bigQuery是从文件加载的,因为当我尝试加载 @bigquery 时与
@bigquery = @client.discovered_api('bigquery', 'v2')
我得到Google::APIClient::ClientError: Not Found并仅检查打印
#<Google::APIClient::API:0x17c94cc ID:bigquery:v2>

但是,如果我将 Big Query API 保存为文本文件 https://www.googleapis.com/discovery/v1/apis/bigquery/v2/rest

然后将其加载为文本文件

doc = File.read('bigQueryAPI.json')
@bigQuery = @client.register_discovery_document('bigquery', 'v2', doc)

然后@bigQuery.inspect实际上返回了一些有用的东西。 @bigQuery.inspect output .

但是,当我尝试实际运行查询时,如下所示:

result = @client.execute!(
           :api_method => @bigQuery.batch_path.query,
           :body_object => { "query" => "SELECT count(DISTINCT repository_name) as repository_total, " +
                                 "count(payload_commit) as commits_total, " +
                                 "count(DISTINCT repository_name) / count(payload_commit) as average, " +
                                 "FROM [githubarchive:github.timeline]" }, #,
           :parameters => { "projectId" => @project_id })

我收到以下错误:

NoMethodError: undefined method `query_values' for nil:NilClass

这是错误的完整堆栈跟踪:

    1) Error:  
test_averages(Test_GitHub_Archive):   
NoMethodError: undefined method `query_values' for nil:NilClass  
   C:/Ruby193/lib/ruby/gems/1.9.1/gems/google-api-client-0.7.1/lib/google/api_client/request.rb:145:in `uri=' 
   C:/Ruby193/lib/ruby/gems/1.9.1/gems/google-api-client-0.7.1/lib/google/api_client/request.rb:101:in `initialize'  
   C:/Ruby193/lib/ruby/gems/1.9.1/gems/google-api-client-0.7.1/lib/google/api_client.rb:518:in `new'  
   C:/Ruby193/lib/ruby/gems/1.9.1/gems/google-api-client-0.7.1/lib/google/api_client.rb:518:in `generate_request'  
   C:/Ruby193/lib/ruby/gems/1.9.1/gems/google-api-client-0.7.1/lib/google/api_client.rb:583:in `execute!'  
   C:/Users/tfburton/Documents/private/ProjectSuggestor/RubyStats/GitHub_Archive.rb:39:in `get_averages'  
   C:/Users/tfburton/Documents/private/ProjectSuggestor/RubyStats/TestSpec/test_GitHub_Archive.rb:26:in `test_averages'  

这是 @client.inspect 的结果
注意:我本想粘贴到这里,但我的帖子超出了长度限制。

经过一番挖掘。看起来我没有通过正确的 @bigQuery参数来获取查询功能。
查看转储中的 @bigQuery.inspect我需要传递 751 行的方法。
但是我似乎不知道如何传递该方法。

如果去掉检查输出的其余部分,“路径”如下所示:
{ "resources => { "jobs" => { "methods" => { "query"

我已经尝试过@bigQuery.Jobs.query这会导致错误指出 @bigQuery.Jobs不存在。

  • 我也在创建 @bigQuery正确吗?
  • 为什么不@bigQuery.Jobs.query工作?

最佳答案

以下是我如何让它与 bigquery.jobs.query 方法一起工作,这可能正是您所需要的。

我必须设置 OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE ,否则授权过程会严重失败,但这可能特定于最小 Win7/MgitSys 环境。无论如何,这个特定的行在产品中并不安全。

require 'google/api_client'
require 'google/api_client/client_secrets'
require 'google/api_client/auth/installed_app'
require 'google/api_client/auth/file_storage'
require 'openssl'
require 'json'

OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE 

# Initialize the client.
client = Google::APIClient.new(
  :application_name => 'Example Ruby application',
    :application_version => '1.0.0'
    )

    CREDENTIAL_STORE_FILE = "#{$0}-oauth2.json"
     file_storage = Google::APIClient::FileStorage.new(CREDENTIAL_STORE_FILE)
    # Initialize Google+ API. Note this will make a request to the
    # discovery service every time, so be sure to use serialization
    # in your production code. Check the samples for more details.
    @bigQuery = client.discovered_api('bigquery', 'v2')

    # Load client secrets from your client_secrets.json.
    client_secrets = Google::APIClient::ClientSecrets.load

     file_storage = Google::APIClient::FileStorage.new(CREDENTIAL_STORE_FILE)
      if file_storage.authorization.nil?
        client_secrets = Google::APIClient::ClientSecrets.load
        # The InstalledAppFlow is a helper class to handle the OAuth 2.0 installed
        # application flow, which ties in with FileStorage to store credentials
        # between runs.
        flow = Google::APIClient::InstalledAppFlow.new(
          :client_id => client_secrets.client_id,
          :client_secret => client_secrets.client_secret,
          :scope => ['https://www.googleapis.com/auth/cloud-platform','https://www.googleapis.com/auth/bigquery']
        )
        client.authorization = flow.authorize(file_storage)
      else
        client.authorization = file_storage.authorization
      end



      puts "authorized, requesting"

      # Make an API call.
      result = client.execute!(
           :api_method => @bigQuery.jobs.query,
           :body_object => { "query" => "SELECT count(DISTINCT repository_name) as repository_total, " +
                                 "count(payload_commit) as commits_total, " +
                                 "count(DISTINCT repository_name) / count(payload_commit) as average, " +
                                 "FROM [githubarchive:github.timeline]" }, #,
           :parameters => { "projectId" => "845227657643" })

          puts  JSON.dump result.data

关于ruby - 为什么在使用 google API 对 google Big Query 运行查询时出现 NoMethodError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23309015/

相关文章:

Ruby、Qt 和第三方小部件

php - Google 客户端 JWT 无效 : Token must be a short-lived token

python - 无法使用 python 基于 BQ 中的查询创建另一个表

ruby - 错误 : Installing vagrant gem in ruby 2. 0.0

Ruby SHA1 标志返回与 OpenSSL 标志不同的结果

javascript - 设置 Google Plus 登录按钮类型

mysql - 如何构建一个稳定的系统,每天处理 9000 万行并传输到实时服务器?

java - BigQuery - 通过 java 进行流式传输非常慢

ruby-on-rails - 从另一个 gem 获取一个 gem 的 Assets

c - 是否有用于 OpenAuth 或任何其他社交网络身份验证方法的嵌入式 API?