Thanks to a tweet from Lak, he highlighted another way to authenticate purely online when using notebooks e.g. not uploading a token from an offline R session.
Lak documents this in this helpful blogpost
It involves using the Terminal to get user access from the authentication within GCP.
OAuth2 client ID (Other)
for the project running the notebook at this link https://console.cloud.google.com/apis/credentialsjupyter_client_id.json
)File > New > Terminal
and issue the below commands to login via the browser, and print the access token (hint: you can press SHIFT and right click in terminal to copy/paste if CTRL+C and CTRL+V# https://cloud.google.com/sdk/gcloud/reference/auth/application-default/login
gcloud auth application-default login \
--client-id-file jupyter_client_id.json \
--scopes=https://www.googleapis.com/auth/analytics.readonly,https://www.googleapis.com/auth/analytics
## access the URL, login and create a verification code, paste in console.
## view then copy-paste the access token, to be passed into the R function
gcloud auth application-default print-access-token
googleAuthR
version > 0.7.9000gar_gce_auth_default <- function(access_token,
scopes,
cache_file = "gcloud.auth"){
json_creds <- jsonlite::fromJSON('~/.config/gcloud/application_default_credentials.json')
token_formatted <-
httr::Token2.0$new(app = httr::oauth_app("google",
key = json_creds$client_id,
secret = json_creds$client_secret),
endpoint = httr::oauth_endpoints("google"),
credentials = list(access_token = access_token,
token_type = json_creds$type,
expires_in = NULL,
refresh_token = NULL),
params = list(scope = scopes, type = NULL,
use_oob = FALSE, as_header = TRUE),
cache_path = FALSE)
options("googleAuthR.client_id" = json_creds$client_id,
"googleAuthR.client_secret" = json_creds$client_secret,
"googleAuthR.scopes.selected" = scopes)
saveRDS(token_formatted, cache_file)
message("Authenticated. Token saved to ", cache_file)
token_formatted
}
An example token copy-pasted from the terminal is below. Pass that and the scopes for the API into the function above to create the token
at='ya29.XXXXXOgi2Zyl0PYU-mwZseSWXXXXXiQqxwCCalVMgRMcoXRn5fF2PH_Rz4fltI16O10zgh-RVN'
scopes = c('https://www.googleapis.com/auth/analytics.edit,https://www.googleapis.com/auth/analytics')
token <- gar_gce_auth_default(at, scopes = scopes)
Once the token is available, you can use pass it in directly in the ga_auth()
function, or via the filename it has been saved under (by default gcloud.auth
)
library(googleAnalyticsR)
ga_auth(token)
# can now list accounts, get data etc.
accs <- ga_account_list()
head(accs)
viewId <- 81416156
gadata <- google_analytics(viewId,
date_range = c("30daysAgo", "yesterday"),
metrics = "sessions", dimensions = "source",
order = order_type("sessions", "DESCENDING", "VALUE"))
head(gadata)
One other alternative which is better for long-term production use it to generate a service account key.
service account key
heregoogleAuthR::gar_service_auth()
to authenticate.This has more details on the googleAnalyticsR website here
sessionInfo()