'Force R googlesheets4 gs4_auth() to use specified user account after using service account
I am working on a simple script that uses googlesheets4
to read some data and then uses gargle to authenticate an API call. I am running into an issue where gs4_auth()
authenticates the wrong user.
I first run gs4_auth("[email protected]")
to authenticate with my Google Account and everything works find.
Later, I authenticate my API calls by generating a token using gargle::credentials_service_account()
with a local service account json key. That also works fine.
The issue arises when I try to run the script again after previously authenticating the API calls. When I run gs4_auth()
it uses the service account credentials and the read_sheet()
call returns a 403 error.
Is there a way to force gs4_auth()
not to use the service account credential. The service account has a different email than the email passed to gs4_auth()
so I would not expect it to use the non-matching credential.
I ran the authentication with options(options(gargle_verbosity = "debug")
and it provided the following output:
> gs4_auth(email = '[email protected]')
trying `token_fetch()`
trying `credentials_service_account()`
Error caught by `token_fetch()`:
Argument 'txt' must be a JSON string, URL or file.
trying `credentials_external_account()`
aws.ec2metadata not installed; can't detect whether running on EC2 instance
trying `credentials_app_default()`
file exists at ADC path:
/Users/path-to-service-account-credentials/json-key-filename.json
ADC cred type: "service_account"
trying `credentials_service_account()`
adding "userinfo.email" scope
service account email: [email protected]
The subsequent 403 error occurs because [email protected] has access to the google sheet but [email protected] does not.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|