为什么我在使用谷歌API针对谷歌大查询运行查询时得到NoM​​ethodError?

时间:2014-04-26 09:29:40

标签: ruby google-api google-bigquery

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

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

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

ENVIORNMENT:

  • 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 行传递方法。
但是,我似乎无法弄清楚如何通过该方法。

如果你去除检查输出的其余部分,那么&#34;路径&#34;看起来像这样:
{ "resources => { "jobs" => { "methods" => { "query"

我已经尝试了@bigQuery.Jobs.query,这导致错误表明@bigQuery.Jobs不存在。

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

1 个答案:

答案 0 :(得分:2)

以下是我如何使用bigquery.jobs.query方法,这可能就是你所需要的。

我必须设置 OpenSSL :: SSL :: VERIFY_PEER = OpenSSL :: SSL :: VERIFY_NONE ,否则授权过程会失败,但这可能是针对min 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