在 R 中刷新 Google 分析令牌

Refresh Google Analytics token in R

我正在使用以下代码在 R 中检索 Google Analytics 令牌。 当我获得原始令牌时它会起作用,但是一旦它过期我就无法刷新它。我得到

Error: Refresh token not available

ValidateToken(token) 之后。我做错了什么?

#get and store token 
require("RGoogleAnalytics")
token <- Auth(client.id,client.secret)
save(token,file="./token_file")

#Get refresh token
load("token_file")
ValidateToken(token)

我必须为这个案例做出完整的回答。

RGA 自动刷新令牌,除非您指定不这样做。

运行 这行,并使用 View(ga_profiles) 来获得你需要的正确 ID。它是视图的 ID(不是帐户 ID)。在ga_profiles中,是第一列。

library(RGA)

# get access token
authorize()

# get a GA profiles

ga_profiles <- list_profiles()

如果您需要(或想要)RGA 每次提取数据时都请求权限,请使用此代码:

注意 new.auth = TRUE 参数。

library(RGA)

# get access token
authorize(new.auth = TRUE)

# get a GA profiles

ga_profiles <- list_profiles()

并使用来自 Google Analytics 的数据制作一个简单的 df:

id <- 88090889 #This ID is the first column from ga_profiles. Not the Account ID.

# get date when GA tracking began
first.date <- firstdate(id)


# get GA report data
ga_data <- get_ga(id, start.date = first.date, end.date = "today",
                  metrics = "ga:users,ga:sessions",
                  dimensions = "ga:userGender,ga:userAgeBracket")

如果您需要更多帮助,post您的代码,我们会看到。