R googlesheets4将令牌作为参数传递给read_sheet

问题描述

只有在工作表已被授权的情况下,我才能阅读工作表

如果不是,则忽略每行后续代码,因为代码执行不断询问令牌。有没有办法将令牌索引传递到read_sheet命令中,或者有某种方法可以预先授权访问?

   > 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: 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

解决方法

您需要进行auth调用,以提供不同的身份验证方法:

默认身份验证

尝试不带参数或与Google帐户关联的电子邮件地址的默认呼叫:

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

服务帐户

您还可以传递与服务帐户关联的令牌文件:

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

path-JSON标识服务帐户,采用jsonlite :: fromJSON()的txt参数支持的格式之一(通常是文件路径或JSON字符串)。

漱口水令牌

googlesheets4使用gargle包,因此您也应该能够像这样获得令牌:

library(gargle)
token <- token_fetch()
googlesheets4::gs4_auth(token = token)