tidyverse / googlesheets4

Google Spreadsheets R API (reboot of the googlesheets package)
https://googlesheets4.tidyverse.org
Other
360 stars 53 forks source link

How to complete gs4 auth () function? #226

Closed sebastianjhm closed 3 years ago

sebastianjhm commented 3 years ago

Hi, thanks to the googlesheets4 team, it's an excellent library. I am new to this world of APIs and I have not found much information about it. My intention is to write a shiny application that writes data in a spreadsheet, but I don't know how to authenticate with Gargle, that is, I don't even know how to extract the token that gives the application permissions to access the file or how to use it. . I don't know if I have to extract it using GCP or from Drive. I would like to know how to fill the gs4_auth () to be able to read and write in the spreadsheet. I should clarify that I would like only the application to have the permissions to access the data. Thank you very much in advance and apologies if it is too obvious.

cpilat97 commented 3 years ago

If you're using gs4_auth to reference your own resources, you'll need to set up an API credentials.

Here's a link to a gargle page that walks you through which API credentials you can set up, how to set it up in the cloud. You'll need to choose which works best for your case. From there, depending on your setup, you'll have to call gs4_auth(....) and reference a file in your project (or wherever you've put it on your computer).

If you don't set up your own API credentials, you'll be using the public googlesheets4 resources (unless that's been changed) which may create a user limit error depending on how many people are accessing that resource at the same time.

For example, I use a service account token which is stored as a .JSON file on my computer. So at the beginning of my script i'll use gs4_auth(path = "path/to/my/service-account-token.json"

Hope this is helpful!

Another useful link

jennybc commented 3 years ago

I think, like @cpilat97's example, that you @SebastianJHM should use a service account. This requires some setup, which will be unfamiliar and perhaps uncomfortable at first, but then you will have a robust auth solution for your app.

The links above are sound re: how to get a service account and then how to put its credentials into force.

Remember that you will need to explicitly give this new service account write permission on the target Sheet. The service account won't somehow inherit these privileges automagically, simply because you have such privileges and you are associated with the GCP project that owns the service account. It doesn't work that way, but this appears to be a common gotcha.