rauthenticationbigrquery

How to authenticate in Google Big Query using Bigrquery without user input using a service account


I need to run an R script using windows task scheduler, however the script includes an authentication to Big Query using Bigrquery. I have the service account authentication. When I run the script I'm asked for user input which doesn't let me run the script as scheduled. I get:

The bigrquery package is requesting access to your Google account. 
Select a pre-authorised account or enter '0' to obtain a new token. Press 
Esc/Ctrl + C to abort

1. ("Account I've used to authenticate")

Here my code:

library(googleAuthR)
library(bigrquery)

scopes = c("https://www.googleapis.com/auth/bigquery")


options(googleAuthR.scopes.selected = scopes)
service_token <- gar_auth_service(json_file="somejason.json")
gar_auth_service("somejason.json")


projectid<-'project_id'
datasetid<-'id'
bq_conn <-  dbConnect(bigquery(), 
                      project = projectid,
                      dataset = datasetid, 
                      use_legacy_sql = FALSE
)

tables_list <- dbListTables(bq_conn)

The bigrquery package is requesting access to your Google account. Select 
a pre-authorised account or enter '0' to obtain a new token. Press 
Esc/Ctrl + C to abort.

1: ("Account I've used to authenticate")

Is there a way to authenticate with the service account without needing user input?


Solution

  • So I read How to authenticate with service account and bigrquery package? .

    There set_service_token(path='somejson.json') is suggested, but it's depricated. Instead, I used bq_auth(path='somejson.json').