How to manually create a gargle/googlesheets4 authentication token?

With Google OOB being depreciated we are currently looking for alternatives for getting and refreshing tokens from a pure CLI environment (i.e. remote server with no browser installed). We currently have a prototype solution based on a rsconnect webapp that authenticates and stores the tokens for the CLI to retrieve later which seems to be working. What I was hoping to get help with is how to convert tokens received from this webserver into tokens that gargle/googlesheets4 can use.

i.e. from our webserver we get a list object that looks something like this:

credentials <- list(
    expiry = "2022-06-27T15:28:09.215485Z",
    refresh_token = "<REFRESH_TOKEN_STRING>",
    scopes = "https://www.googleapis.com/auth/spreadsheets.readonly",
    token = "<TOKEN_STRING>",
    token_uri = "https://oauth2.googleapis.com/token"
)

How can I convert this into a gargle token object that googlesheets4 can use ?

The following code is something that I managed to hack together which seems to work however I don't really understand the internals of gargle and am worried I am abusing it in a way it wasn't designed for. Any guidance on how to do this "properly" would be super appreciated.

credentials2 <- credentials
credentials2$access_token <- credentials2$token
credentials2$token <- NULL
credentials2$refresh_token <- NULL
token <- httr::Token2.0$new(
    endpoint = httr::oauth_endpoints("google"),
    app = httr::oauth_app("null", secret = "null", key = "null"),
    params = list(as_header = TRUE),
    cache_path = FALSE,
    credentials = creds
)
gargle::cred_funs_set(list(gargle::credentials_byo_oauth2))
googlesheets4::gs4_auth(token = token)
googlesheets4::read_sheet(...)

Anyone able to help ?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.