StevenMMortimer/rdfp

Failed to create local cache ('.httr-oauth')

Closed this issue · 3 comments

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:

library("rdfp")
library(magrittr)
library(formattable)

options(rdfp.network_code = "xxxxxxx",
        rdfp.application_name = "MyAppDFP",
        rdfp.client_id = "xxxxxxxxx-yyyyy.apps.googleusercontent.com",
        rdfp.client_secret = "xxxxxx",
        rdfp.httr_oauth_cache = TRUE)

dfp_auth()
today <- Sys.Date()
yesterday_day = format(as.Date(today-1,format="%Y-%m-%d"), "%d")
yesterday_month = format(as.Date(today-1,format="%Y-%m-%d"), "%m")

request_data <- list(reportJob=list(reportQuery=list(dimensions='AD_EXCHANGE_TAG_NAME',
                                                     adUnitView='TOP LEVEL',
                                                     columns='AD_EXCHANGE_REQUESTS',
                                                     columns="AD_EXCHANGE_MATCHED_QUERIES",
                                                     columns="AD_EXCHANGE_COVERAGE",
                                                     columns="AD_EXCHANGE_CLICKS",
                                                     columns="AD_EXCHANGE_MATCHED_QUERIES_CTR",
                                                     columns="AD_EXCHANGE_CPC_REVENUE",
                                                     columns="AD_EXCHANGE_REQUEST_ECPM",
                                                     columns="AD_EXCHANGE_ESTIMATED_REVENUE",
                                                     startDate=list(year=2017, month=yesterday_month, day=yesterday_day),
                                                     endDate=list(year=2017, month=yesterday_month, day=yesterday_day),
                                                     dateRangeType='CUSTOM_DATE')))

report_data_yesterday <- dfp_full_report_wrapper(request_data)

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.

@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')