Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I had the same problem and I solved it like this:</p> <p><strong>Google Account Setup:</strong></p> <ol> <li>Go to Google Apis (<a href="https://code.google.com/apis/console/" rel="nofollow">https://code.google.com/apis/console/</a>).</li> <li>Under "Services" enable the service that you would like to use (e.g. Analytics API).</li> <li>Under "API Access" create a OAuth Client ID by clicking on the big blue button.</li> <li>On the next screen enter your product information (name, logo, url).</li> <li>On the next screen, because the communication will handled by the server, we check the "Service Account" option and click the Create Client ID button.</li> <li>Download the private key that was just generated and put it to ({Rails.root}/config/.p12).</li> <li>Leave the API Access page opened because you will need some parameter values in the next step.</li> <li>In new tab, go to <a href="https://developers.google.com/products/" rel="nofollow">https://developers.google.com/products/</a> and choose the service that you would like to use. In the left menu navigate to Configuration > Management API (v3) > Resources > Authorization (<a href="https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtAuthorization" rel="nofollow">https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtAuthorization</a>). In the table below you will find the appropriate ":scope" parameter url (e.g. <a href="https://www.googleapis.com/auth/analytics.readonly" rel="nofollow">https://www.googleapis.com/auth/analytics.readonly</a>). This is how you find the appropriate scope.</li> <li>Note that the ":issuer" parameter is the email address of the service account that's displayed next to the Client ID (e.g. @developer.gserviceaccount.com).</li> <li>For the checking the URL (e.g. <a href="https://developers.google.com/youtube/analytics/v1/" rel="nofollow">https://developers.google.com/youtube/analytics/v1/</a> would be youtubeAnalytics, v1). I usualy find that googling examples :).</li> <li>Note that the is the "Public key fingerprints" value.</li> <li>Do check the source code at <a href="https://github.com/google/google-api-ruby-client" rel="nofollow">https://github.com/google/google-api-ruby-client</a> for each object returned.</li> <li>Maybe you will have to add the issuer email address to your Google Analytics Account.</li> </ol> <p><strong>Ruby on Rails Code:</strong></p> <pre><code># creating client instance client = Google::APIClient.new # authenticating key = Google::APIClient::PKCS12.load_key("#{Rails.root}/config/&lt;STRANGE_LONG_FILENAME&gt;.p12", 'notasecret') client.authorization = Signet::OAuth2::Client.new( :token_credential_uri =&gt; 'https://accounts.google.com/o/oauth2/token', :audience =&gt; 'https://accounts.google.com/o/oauth2/token', :scope =&gt; '&lt;SCOPE_URL&gt;', :issuer =&gt; '&lt;HASH&gt;@developer.gserviceaccount.com', :signing_key =&gt; key) client.authorization.fetch_access_token! # API call # NOTE: Check the documentation for API methods and parameters). The method discovered_api returns a service object. We can use to_h.keys to get the list of available keys of that object. Keys represents API methods (e.g. "analytics.management.accounts.list" the API method path is "management.accounts.list"). result = client.execute( :api_method =&gt; client.discovered_api('&lt;SERVICE_NAME&gt;', 'v3').management.accounts.list, :parameters =&gt; { accountId: '~all', webPropertyId: '~all'} ) if result.success? result.data end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload