Ruby Google Analytics API出错

Ruby Google Analytics API出错,ruby,google-analytics,dashing,Ruby,Google Analytics,Dashing,我正在尝试使用它的Ruby文件 require 'google/api_client' require 'date' # Update these to match your own apps credentials service_account_email = '[YOUR SERVICE ACCOUNT EMAIL]' # Email of service account key_file = 'path/to/your/keyfile.p12' # File

我正在尝试使用它的Ruby文件

require 'google/api_client'
require 'date'


    # Update these to match your own apps credentials
    service_account_email = '[YOUR SERVICE ACCOUNT EMAIL]' # Email of service account
    key_file = 'path/to/your/keyfile.p12' # File containing your private key
    key_secret = 'notasecret' # Password to unlock private key
    profileID = '[YOUR PROFILE ID]' # Analytics profile ID.

    # Get the Google API client
    client = Google::APIClient.new(:application_name => '[YOUR APPLICATION NAME]', 
      :application_version => '0.01')

    # Load your credentials for the service account
    key = Google::APIClient::KeyUtils.load_from_pkcs12(key_file, key_secret)
    client.authorization = Signet::OAuth2::Client.new(
      :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
      :audience => 'https://accounts.google.com/o/oauth2/token',
      :scope => 'https://www.googleapis.com/auth/analytics.readonly',
      :issuer => service_account_email,
      :signing_key => key)

    # Start the scheduler
    SCHEDULER.every '1m', :first_in => 0 do

      # Request a token for our service account
      client.authorization.fetch_access_token!

      # Get the analytics API
      analytics = client.discovered_api('analytics','v3')

      # Start and end dates
      startDate = DateTime.now.strftime("%Y-%m-01") # first day of current month
      endDate = DateTime.now.strftime("%Y-%m-%d")  # now

      # Execute the query
      visitCount = client.execute(:api_method => analytics.data.ga.get, :parameters => { 
        'ids' => "ga:" + profileID, 
        'start-date' => startDate,
        'end-date' => endDate,
        # 'dimensions' => "ga:month",
        'metrics' => "ga:visitors",
        # 'sort' => "ga:month" 
      })

      # Update the dashboard
      send_event('visitor_count',   { current: visitCount.data.rows[0][0] })
    end
但是,在最后一行的第二行,我得到了nil:NilClass的错误
未定义的方法“[]”。有人能解释一下这里发生了什么吗

编辑:
我现在知道visitCount.data是一个由NIL对象组成的数组。我是否可以执行任何诊断以确保API正确连接?有人能提出发生这种情况的可能原因吗?

在流媒体播放事件之前,尝试一下这个

if visitCount.data.rows[0].empty?
  # assign some default
  output = -1
else
  output = visitCount.data.rows[0][0] 
end

# Update the dashboard
send_event('visitor_count',   { current: output })

好的,结果是我得到了不正确的配置文件ID。令人惊讶的是,没有错误地说“这个配置文件ID不存在”。那会更有帮助。无论如何,我现在有了正确的配置文件ID,它可以工作了。如果有人想知道,要找到配置文件ID,您可以导航到您感兴趣的分析配置文件,url的末尾有类似于PXXXXXXX的内容,其中X是您的配置文件ID。

要么
visitCount.data.rows
要么
visitCount.data.rows[0]
返回
nil
。第一步是找出哪个以及为什么。看起来visitCount.data.rows是一个数组对象,visitCount.data.rows[0]是一个NilClass对象。我尝试了此操作,但返回了相同的错误,这意味着visitCount不是零。但是,根据我的评论,visitCount.data.rows[0]是一个nil对象。上面的一个使用empty如何?