Last active
April 25, 2017 10:55
-
-
Save kutasok/42b8fd9dba8db8d6e2d70d08eb0eb75f to your computer and use it in GitHub Desktop.
Google auth by servise account without json file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library("googleAuthR") | |
library("googleAnalyticsR") | |
library("jsonlite") | |
# Сюда вставить то, что в json-файле. Обязательно заменить обратный слеш \ на двойной \\ | |
json <- 'credentials from json' | |
# функция для авторизации | |
gar_auth_service <- function(json, scope = getOption("googleAuthR.scopes.selected")){ | |
endpoint <- httr::oauth_endpoints("google") | |
secrets <- jsonlite::fromJSON(json) | |
scope <- paste(scope, collapse=" ") | |
if(is.null(secrets$private_key)){ | |
stop("$private_key not found in JSON - have you downloaded the correct JSON file? | |
(Service Account Keys, not service account client)") | |
} | |
google_token <- httr::oauth_service_token(endpoint, secrets, scope) | |
Authentication$set("public", "token", google_token, overwrite=TRUE) | |
Authentication$set("public", "method", "service_json", overwrite=TRUE) | |
return(invisible(Authentication$public_fields$token)) | |
} | |
# авторизируемся | |
gar_auth_service( | |
json = json, | |
scope = "https://www.googleapis.com/auth/analytics" | |
) | |
# id представления GA | |
ga_view <- 'id' | |
# Выгружаем данные | |
gadata <- google_analytics(id = ga_view, | |
start="2016-01-01", | |
end="2016-12-31", | |
metrics = 'ga:sessions', | |
dimensions = "ga:yearMonth", | |
sort = 'ga:yearMonth', | |
filters = "ga:medium==organic", | |
max_results = 10000 | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment