rgoogle-sheetsshinygooglesheets4

Authorizing non-interactive use of googlesheets through Shiny app using googlesheets4


I know there are a few other similar questions floating around SO (e.g., here and here), but I thought I'd try again, as the others have not gotten many answers.

I have a Shiny app that allows its users to provide input through the app, but these users need to skip the authentication step. In the previous version of googlesheets I made the sheet public (to anyone with the link) and avoided some of the authentication process. In updating to googlesheets4 I've run into permissions issues with deploying the updated app to shinyapps.io. Here is the summary of what I've tried, and what results I've gotten. Any pointers would be much appreciated.

First, based on the recommendations in this gargle article I created a service account, downloaded a JSON service account token (with format "project-name-12345678abc1.json"), and have saved it (for now) in the parent app folder, under the assumption that it needs to be uploaded with the app bundle. Then I enabled the Google Sheets API.

I've put the following commands in the front of the app, ahead of anything substantial:

  1. gs4_deauth(), since the google sheet is public to anyone with the link and therefore might not require a token.

  2. Following this article, gs4_auth(path = "project-name-12345678abc1.json"). I've also added the scopes argument (as below), and use_oob=TRUE.

  3. credentials_service_account(scopes = "https://www.googleapis.com/auth/spreadsheets", path = "project-name-12345678abc1.json").

  4. credentials_app_default(path = "project-name-12345678abc1.json")

Results

Most combinations give me errors saying "Can't get Google credentials" and then advising that I look at the non-interactive article on the gargle site. The one exception is Attempt 3, which provides the following error message instead - strangely promising:

Warning: Error in : Client error: (403) PERMISSION_DENIED

  • Client does not have sufficient permission. This can happen because the OAuth token does not have the right scopes, the client doesn't have permission, or the API has not been enabled for the client project.

Any ideas on what I might be missing? The app works fine locally.

Any help is appreciated. Thanks!


Solution

  • This problem has been addressed using the approach identified in this (closed) Github issue.

    The key is to follow these steps exactly:

    1. Call gs4_auth(cache = ".secrets") once, running the app locally in the browser.
    2. Substitute the line above with gs4_auth(cache = ".secrets", email = TRUE, use_oob = TRUE).
    3. Deploy the app to shinyapps.

    I also cleared the previous tokens I had saved, which might have been important, though hard to know.

    As noted in the Github discussion, including use_oob = TRUE is probably not necessary. Additionally, this approach is detailed in the non-interactive auth article on the gargle site.