0

I can read a sheet only if the sheet is already authorised,

If not, every subsequent line of code is ignored, as the code execution keeps asking for the token. Is there a way to pass the token index into the read_sheet command, or some way to authorise access beforehand?

   > aa <- read_sheet('https://docs.google.com/spreadsheets/d....', sheet='Akinesia', range="AC:AH")
The googlesheets4 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: chris.elliott@.....

Selection: 
Enter an item from the menu, or 0 to exit
Selection: 
Enter an item from the menu, or 0 to exit
Selection: 
Enter an item from the menu, or 0 to exit
Selection: aaX <- aa  %>% filter_all(any_vars(!is.na(.)))
Enter an item from the menu, or 0 to exit
Selection: akns <- aaX %>% pivot_longer(-genotype, names_to = "day", values_to = "nonresp")
Enter an item from the menu, or 0 to exit
zx8754
  • 52,746
  • 12
  • 114
  • 209

2 Answers2

0

You need to make an auth call which provides different authentication methods:

default auth

Try default call without params or with email address that is associated with a Google account:

googlesheets4::gs4_auth(email = "chris.elliott@...")

service account

you can also pass token file associated with a service account:

googlesheets4::gs4_auth(path = "access_token.json")

path - JSON identifying the service account, in one of the forms supported for the txt argument of jsonlite::fromJSON() (typically, a file path or JSON string).

gargle token

googlesheets4 uses gargle package, so you should be able also to get the token like this:

library(gargle)
token <- token_fetch()
googlesheets4::gs4_auth(token = token)
Bulat
  • 6,869
  • 1
  • 29
  • 52
  • Sorry, these don't help. default auth and service account both sent back the message ```The googlesheets4 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.``` This doesn't help get the saved token automatically, gargle token code just started the Google password dialog. I really wanted to be able to automatically select the men item No1 – Chris Elliott Oct 29 '20 at 15:30
  • Try the first option with email parameter set explicitly – Bulat Oct 29 '20 at 15:47
0

I think the option that @Bulat suggested works, i.e., using the email address associated with your Google account directly within the gs4_auth() call.

Marco
  • 2,368
  • 6
  • 22
  • 48