1

So I'm creating a dashboard using dashing (sinatra based framework) and I need to get data from Analytics API, and display it in the dashboard. So I have the following ruby code, and the oauth returns this error:

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

Here it's the code:

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
Chrez
  • 123
  • 7

1 Answers1

0

I'm not sure how old is that example. The Ruby API seems to be very poorly documented, and since it's still in alpha stage perhaps it changed a bit. I recently did a quick ruby sample and it works great with the latest version of the API 0.9.6

First you want to go into the Google Cloud Console, and export a JSON key file for your service account. Then put it somewhere ruby can read it. You'll need to pass into ENV its path.

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

I find this is the easiest way to query the Ruby API with a Service Account.

Also make sure to grant access to the Analytics account to your service account.

From there you can query other methods documented here.

For realtime specifically you can do:

puts analytics.get_realtime_data("ga:XXXXX", "ga:activeVisitors").totals_for_all_results
Eduardo
  • 21,340
  • 11
  • 72
  • 92
  • I'm a "Ruby Noob" so I can't really understand what should i do. How do i replace your code with mine? I mean... what should i delete of mine? – Chrez May 01 '16 at 09:38
  • You should delete everything. Mine should run as is. Just make sure to have the json key and the latest version of the Google API client. – Eduardo May 01 '16 at 15:09