When I use googlesheets4 in R, I use sheets_auth() in the console and it works fine. But when I try to run it in R markdown, and when I try to knit, I cannot seem to get the credentials. Can someone walk me through the process? I've gone to the vignettes for googlesheets4 but cannot seem to understand it.
Asked
Active
Viewed 245 times
3
-
Have you found a solution? – Diya Aug 20 '20 at 17:57
-
Well no I did not. I ended up just downloading it and using the sheet locally from Excel. – Armin Thomas Aug 21 '20 at 08:05
1 Answers
0
This is working for me
gs4_auth(path = "xxxxxxxxxxxxxxxx.json")
It doesn't return anything, but after that I'm able to write data in my sheet with sheet_write()
To get the credentials in a json file you have to follow these steps:
- From the Developers Console, in the target GCP Project, go to IAM & Admin > Service accounts.
- Give it a decent name and description.
- For example, the service account used to create the googledrive docs has name “googledrive-docs” and description “Used when generating googledrive documentation”.
- Service account permissions. Whether you need to do anything here depends on the API(s) you are targetting. You can also modify roles later and iteratively sort this out.
- For example, the service account used to create the googledrive docs does not have any explicit roles.
- The service account used to test bigrquery has roles BigQuery Admin and Storage Admin.
- Grant users access to this service account? So far, I have not done this, so feel free to do nothing here. Or if you know this is useful to you, then by all means do so.
- Do Create key and download as JSON. This file is what we mean when we talk about a “service account token” in the documentation of gargle and packages that use gargle. gargle::credentials_service_account() expects the path to this file.
- Appreciate that this JSON file holds sensitive information. Treat it like a username & password combo! This file holds credentials that potentially have a lot of power and that don’t expire.
- Consider storing this file in such a way that it will be automatically discovered by the Application Default Credentials search strategy. See credentials_app_default() for details.
- You will notice the downloaded JSON file has an awful name, so sometimes I create a symlink that uses the service account’s name, to make it easier to tell what this file is.
- Remember to grant this service account the necessary permissions on any resources you plan to access, e.g., read or write permission on a specific Google Sheet. The service account has no formal relationship to you as a Google user and won’t automatically inherit permissions.
(copied from here https://gargle.r-lib.org/articles/get-api-credentials.html#service-account-token)

stallingOne
- 3,633
- 3
- 41
- 63