ruby-on-rails - 谷歌分析 API : Required parameter is missing: grant_type

标签 ruby-on-rails ruby google-analytics google-analytics-api dashing

因此,我正在使用 dashing(基于 sinatra 的框架)创建仪表板,我需要从 Analytics API 获取数据,并将其显示在仪表板中。 所以我有以下 ruby​​ 代码,oauth 返回此错误:

{
  "error" : "invalid_request",
  "error_description" : "Required parameter is missing: grant_type"
}

这是代码:

require 'openssl'
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
require 'google/api_client'
require 'date'

# Update these to match your own apps credentials
service_account_email = ENV['chrezworld@dashing-project-1297.iam.gserviceaccount.com'] # Email of service account
key_file = 'key.p12' # File containing your private key
key_secret = 'notasecret' # Password to unlock private key
profileID = ENV['77142386'] # Analytics profile ID.

# Get the Google API client
client = Google::APIClient.new(
  :application_name => ENV['dashing project'], 
  :application_version => '0.01'
)

visitors = []

# 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 '5s', :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')

  # Execute the query
  visitCount = client.execute(:api_method => analytics.data.realtime.get, :parameters => { 
    'ids' => "ga:" + profile_id,
    'metrics' => "ga:activeVisitors",
  })

  visitors << { x: Time.now.to_i, y: visits.to_i }

  # Update the dashboard
  send_event('visitor_count_real_time', points: visitors)

end

最佳答案

我不确定这个例子有多久了。 Ruby API 的文档似乎非常少,而且由于它仍处于 alpha 阶段,也许它发生了一些变化。我最近做了一个快速的 ruby​​ 示例,它与最新版本的 API 0.9.6

配合得很好

首先,您需要进入 Google Cloud Console,并为您的服务帐号导出 JSON key 文件。然后将其放在 ruby​​ 可以读取的地方。您需要将其路径传递给 ENV。

require 'google/apis/analytics_v3'
require 'googleauth'

ENV['GOOGLE_APPLICATION_CREDENTIALS'] = "service_account_credentials.json"

Analytics = Google::Apis::AnalyticsV3
analytics = Analytics::AnalyticsService.new
scopes = [Analytics::AUTH_ANALYTICS_READONLY]
analytics.authorization = Google::Auth.get_application_default(scopes)
accounts = analytics.list_accounts
accounts.items.each do |a| puts a.name end

我发现这是使用服务帐户查询 Ruby API 的最简单方法。

另请确保向您的服务帐户授予对 Google Analytics(分析)帐户的访问权限。

从那里您可以查询记录的其他方法 here

对于实时,你可以这样做:

puts analytics.get_realtime_data("ga:XXXXX", "ga:activeVisitors").totals_for_all_results

关于ruby-on-rails - 谷歌分析 API : Required parameter is missing: grant_type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36955706/

相关文章:

ruby-on-rails - js.erb 中的实例变量

c# - Google Analytics API,将保存的分析过滤器应用于 API 查询

javascript - Google Analytics 片段导致 Linux Firefox 中的页面刷新

ruby-on-rails - 在 rails 4 的 where 条件中使用字符串变量作为字段名称

ruby-on-rails - 访问范围时未定义方法 `default_scoped?'

ruby - 如何通过 ruby​​ 脚本从 Web 服务器请求 gzip 压缩页面?

php - 无法在 Win7 上安装 Heroku,出现错误 "/bin/env: ruby: No such file or directory"

javascript - Rails 中自定义 AJAX 的最佳实践

ruby-on-rails - rails : How to make small changes in different views

google-analytics - 如何通过 GTM 将页面名称传递给 GA?