0

I am trying to use set_service_token in the bigrquery package for a non-interactive authentication.

Here is my code:

library(bigrquery)
set_service_token("client_secret.json")

But it kept showing the error message below:

Error in read_input(file) :
   file must be connection, raw vector or file path

However, when I simply to read the JSON path, it works:

lapply(fromJSON("client_secret.json"), names)

$`installed`
[1] "client_id" "project_id" "auth_uri" "token_uri" "auth_provider_x509_cert_url" "client_secret" "redirect_uris"

Can anyone help me with this? Thank you very much!

Taher A. Ghaleb
  • 5,120
  • 5
  • 31
  • 44
simcheuk
  • 11
  • 1
  • Try using the full location. The error message is pretty clear, what you have there now is not a file name that the function is finding https://stackoverflow.com/questions/42410147/how-to-authenticate-with-service-account-and-bigrquery-package – Elin Dec 10 '18 at 04:42
  • Thank you Elin! I tried the full location just the post you shared. However, I still got the same error message. – simcheuk Dec 10 '18 at 07:08

1 Answers1

1

Looks like your JSON file is in the current directory, but you need the full path to supply the token JSON file. Try this:

json_path <- paste(getwd(), "/client_secret.json", sep="")
set_service_token(json_path)

If that doesn't work, you may try it using the environment variables, like this:

Sys.setenv("CLIENT_SECRET_FILE" = json_path)
set_service_token(Sys.getenv('CLIENT_SECRET_FILE'))

Or, try to supply the JSON content, like this:

set_service_token(toJSON(fromJSON("client_secret.json"), pretty = TRUE))

You may also try using gar_auth_service:

library(googleAuthR)
gar_auth_service(
  json_file = "client_secret.json" # or better use the full path instead
)

Hope it works.

Taher A. Ghaleb
  • 5,120
  • 5
  • 31
  • 44
  • Thank you Teekea for your answer! However, I have already tried both full path and also your code to concatenate a full path. The result is the same. – simcheuk Dec 10 '18 at 06:37
  • Have you tried using the environment variables? Please have a look at my updated answer. – Taher A. Ghaleb Dec 10 '18 at 06:39
  • Or, supply the JSON content. Please have a look at the updated answer. – Taher A. Ghaleb Dec 10 '18 at 06:45
  • Thanks for the quick respond! I have tried the environment variable method but got same result. And then I tried to supply the JSON content directly with your code. I got another error message: Error: Argument 'txt' must be a JSON string, URL or file. But the interesting thing is that when I simply read the JSON file just like what I did at the question, it worked. I am now wondering if I am using the correct JSON file. I downloaded it from GCP --> "APIs & Services" --> "Credentials" --> "OAuth 2.0 client IDs" ( I have set the type as 'Other') – simcheuk Dec 10 '18 at 06:57
  • In your question, you used `fromJSON` to read the JSON file, right? – Taher A. Ghaleb Dec 10 '18 at 07:05
  • Have you tried `toJSON(fromJSON("client_secret.json"), pretty = TRUE)` ? – Taher A. Ghaleb Dec 10 '18 at 07:07
  • Yes I used `fromJSON` to read the file. Also, I tried `toJSON` as you suggested and I am sorry to tell that the error message is the same: Error in read_input(file) : file must be connection, raw vector or file path. – simcheuk Dec 10 '18 at 07:27
  • That's weird. Maybe you need to check whether you are using the correct JSON file for the service you are using. – Taher A. Ghaleb Dec 10 '18 at 07:30
  • Before I give up, I updated my answer with another approach for authentication. Please try it and let me know. – Taher A. Ghaleb Dec 10 '18 at 07:39
  • Oh sorry I think I made a stupid mistake. It seems I used a wrong JSON file. I should be using the JSON file generated from "Service account keys" rather than "OAuth 2.0 client IDs". The codes worked perfectly after the file is replaced. Thank you Teekea for you time and your quick respond! You taught me a lot! – simcheuk Dec 10 '18 at 07:48