Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle server to server auth #54

Open
zippeurfou opened this issue Feb 9, 2016 · 4 comments
Open

handle server to server auth #54

zippeurfou opened this issue Feb 9, 2016 · 4 comments

Comments

@zippeurfou
Copy link

Using google JWT.
i.e. in node js googleapis it works like that:

var google = require('googleapis');
var analytics = google.analytics({
    version: 'v3'
});
var jwtClient = new google.auth.JWT(
    config.ga.client_email,
    null,
    config.ga.private_key, ['https://www.googleapis.com/auth/analytics.readonly'],
    null)

Then you can access your data as follow:

analytics.get({
        'ids': 'ga:123456789',
        'start-date': timestamp_start,
        'end-date': timestamp_start,
        'metrics': 'ga:sessions',
        'dimensions': 'ga:socialActivityContentUrl',
        'sort': '-ga:sessions',
        'max-results': 10000,
        auth: jwtClient
    })

Therefore, you only need an email and private key and it does the rest for you (no need to auth via a webpage).

@jdeboer
Copy link
Owner

jdeboer commented Jan 13, 2018

I'm aiming to use the googleAnalyticsR package as the back-end API client for ganalytics. This will offer added benefits from that package including service account authentication.

@jdeboer
Copy link
Owner

jdeboer commented Jun 23, 2018

@MarkEdmondson1234 and @zippeurfou , now that ganalytics is becoming more closely integrated with googleAnalyticsR, I think looking for ways to incorporate support for googleAuthR into ganalytics would be a sensible next step. It would be great to get your help with that.

@MarkEdmondson1234
Copy link
Collaborator

The token created is the same as via httr, just via the gar_service_auth() function - http://code.markedmondson.me/googleAuthR/reference/gar_auth_service.html

@jdeboer
Copy link
Owner

jdeboer commented Mar 14, 2019

The dev branch now has support for service account authentication. Here is a link to the commit with the necessary changes: 4fb9738

I'll merge this into the master branch at some later point as I may be making some further improvements around this.

Here's a demo, if you would like to test it out, of using a service account to access the Real Time reporting API:

# It is recommended that you restart your R session before continuing with
# the following.
# Install the dev version of ganalytics from GitHub - be patient as this may
# take around 5 minutes to complete.
install.packages("remotes")
remotes::install_github("jdeboer/ganalytics@dev")

# Load ganalytics
library(ganalytics)

# Set credentials - remember to change the secrets filepath to your own service
# account secrets json file. You should obtain this file from the API credentials
# manager in Google Cloud Conosle.
creds <- GoogleApiCreds(secrets = "~/api-service-account-secrets.json")

# Define your query - remember to set the view ID to your Google Analytics view
# that you want to query.
my_rt_query <- RtQuery(
  view = 117987738,
  metrics = "activeUsers",
  dimensions = "userType"
)

# As the GA Real Time API is still in limited beta, you need to sign up for
# access via this form before you execute your query:
# https://docs.google.com/forms/d/1qfRFysCikpgCMGqgF3yXdUyQW4xAlLyjKuOoOEFN2Uw/viewform

# Execute the query with the supplied credentials.
GetGaData(my_rt_query, creds = creds)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants