Closed cristianodalfarra closed 6 years ago
@cristianodalfarra You might want to read this article about OAuth tokens: https://rawgit.com/jennybc/googlesheets/master/vignettes/managing-auth-tokens.html
If you'd like to run the script on a cron or other automated fashion, you can copy/paste the oauth token into the working directory of your script and when it runs with cron it will automatically be authenticated. Also, double check that you have write access to the current working directly so the .httr-auth
file can be created.
@ReportMort thanks for your replay and shared document, it is really interesting.
I'm not sure if I've done well, but I'm using this code for the google authentication for your library and googledrive (and it works in local).
The following code I used once, just to create the .rds file:
file.remove('.httr-oauth') # Remove current token
library(httr)
oauth2.0_token(
endpoint = oauth_endpoints("google"),
app = oauth_app(
"google",
key = getOption("rdfp.client_id"),
secret = getOption("rdfp.client_secret")
),
scope = c(
"https://spreadsheets.google.com/feeds",
"https://www.googleapis.com/auth/dfp",
"https://www.googleapis.com/auth/drive"),
use_oob = FALSE,
cache = TRUE
)
token <- dfp_auth(cache = TRUE)
saveRDS(token, file = "DFP_token.rds")
Then i used dfp_auth(token = "DFP_token.rds")
for authentication.
Happy to know any feedback.
@cristianodalfarra The package stores the cached credentials as a hidden .state
variable. If you're going to authenticate to multiple services, you'll need to overwrite the credentials cached in rdfp:::.state$token
which is where dfp_auth()
puts them.
Below is a function I've created that will create a token to authorize against, sheets, sites, drive, ananlytics, and doubleclick. You could extend this function to meet your needs. It overwrites the packages's .state
variable to ensure it will be referencing the right token and have access to all the google resources you need.
auth_google_services <- function(services = c('sheets',
'sites',
'drive',
'analytics',
'dfp'),
key = key,
secret = secret){
which_services <- match.arg(services, several.ok = TRUE)
supported_scopes <- c(sheets = "https://spreadsheets.google.com/feeds",
sites = "https://sites.google.com/feeds/",
drive = "https://www.googleapis.com/auth/drive",
analytics = "https://www.googleapis.com/auth/analytics",
dfp = "https://www.googleapis.com/auth/dfp")
myapp <- oauth_app("google",
key = key,
secret = secret)
google_endpoint <- oauth_endpoints("google")
new_token <- oauth2.0_token(google_endpoint,
myapp,
scope = unname(supported_scopes[which_services])
assign("token", new_token, envir=rdfp:::.state)
# Some packages, like RGoogleAnalytics requires you to
# pass the token directly, so we'll return it invisibly
invisible(new_token)
}
Example Usage:
token <- auth_google_services(services=c('drive', 'sheets', 'dfp'),
key='xxxx',
secret='xxxx')
Hi all, when I run the .R script from command line I see this error:
"Failed to create local cache ('.httr-oauth')"
Then when I request the report data in R gui (dfp_full_report_wrapper), I see this error:
Error: Refresh token not available
Here is the code I use:
I would like to use in batch mode, with
rdfp.httr_oauth_cache = FALSE
works.Any suggestions would be really appreciated.
thanks in advance
C.