问题描述
我正在使用 googlesheets4 包,它使用 gargle 进行身份验证。我有一个定期运行的预定脚本,所以我需要设置非交互式身份验证。我尝试按照我找到的所有说明设置服务帐户令牌并使用它进行身份验证,但似乎仍然无法使其正常工作。
我创建了一个服务帐户令牌并将其存储在一个 JSON 文件中。然后我使用了以下命令:
gs4_auth (
scopes="https://www.googleapis.com/auth/spreadsheets",path="/path/to/my/service/token.json")
使用 options(gargle_quiet = FALSE)
,我看到以下内容:
trying token_fetch()
trying credentials_service_account()
adding 'userinfo.email' scope
service account email: email_address@project.iam.gserviceaccount.com
似乎一切正常!
但是当我尝试运行 gs <- gs4_find(my_sheet_name)
时,我得到以下信息:
attempt from: googledrive
trying token_fetch()
trying credentials_service_account()
Error: Argument 'txt' must be a JSON string,URL or file.
trying credentials_app_default()
trying credentials_gce()
trying credentials_byo_oauth()
Error: inherits(token,"Token2.0") is not TRUE
trying credentials_user_oauth2()
Gargle2.0 initialize
attempt from: googledrive
adding 'userinfo.email' scope
loading token from the cache
no matching token in the cache
initiating new token
...然后它会打开一个浏览器窗口,要求我登录我的帐户。
似乎 credentials_service_account()
第一次成功,但随后再次调用并失败。我在这里错过了什么?
提前致谢!
解决方法
此问题现已解决,并在 googlesheets4
软件包的 Github 页面上回复了此帖子的副本!
事实证明,gs4_find
实际上是 googledrive::drive_find
的包装器。因此,为了让服务令牌适用于该功能,我需要使用 googledrive
而不是 googlesheets4
进行身份验证。如果您同时使用两者,甚至还有关于协调 googlesheets4
和 googledrive
之间的身份验证的完整 article here。
对于我的特定用例,我实际上只是将 Google Sheet ID 硬编码到我的脚本中,而不是使用 gs4_find
进行名称查找,从而完全避免使用 googledrive。决定这足以满足我的目的。