Auth error with `gmailr` in non-interactive session

I have a script using gmailr that always works running in RStudio and works ~95% of the time running from the command prompt on Windows 10. But every once in a while, it fails with this error:

# Error: Can't get Google credentials.
# Are you running gmailr in a non-interactive session? Consider:
#  * Call `gm_auth()` directly with all necessary specifics.
# Execution halted

It's not exactly a reprex, because it only fails sometimes, but this is what's failing right now:

library(gmailr)
gm_auth_configure(path = here::here("./credentials.json"))
gm_auth(
  email = "My.Actual.Email@gmail.com",
  cache = ".secret",
  path = here::here("./credentials.json")
)

Edit: "./.secret" and "~/.R/gargle/gargle-oauth" are both empty, but they previously weren't. Is there something that makes the tokens (Not sure if that's the right word) in those locations expire or something?

This workflow looks a bit garbled.

Specifically, it's got elements of "regular user OAuth flow" AND using a service account token. So it's basically self-contradictory.

Which auth flow are you intending to use?

What does credentials.json contain? Specifically, is it a service account token or an oauth app (client ID + secret)?

Oh, I'm afraid I don't know the answer to that. I think credentials.json is the file I downloaded at some point in the process of giving the Quickstart application access to my Gmail? It has fields called client_id and client_secret. I'm reading the non-interactive vignette now on the gargle website, and I think it's helpful. I think what happened is my cached credentials kept getting deleted. I'm not sure why that happened still, but I've just backed them up in case it happens again. Thank you so much for your help!

It sounds like credentials.json is an oauth app, then.

In that case, I think you only want to provide it to gm_auth_configure().
And you do NOT want to provide it to gm_auth(..., path = ).
There is no input that is appropriate for both of those function arguments.
I advise that you remove that.

This also seems consistent with everything I see here:

I would only bother to set the cache location (the cache argument to gm_auth()) if you're trying to make something that's deployable, i.e. you plan to move it / run it from another machine.

1 Like

Thanks so much for the help! It all seems to be running smoothly again like this (After running it once interactively and going through the browser authentication process):

library(gmailr)
gm_auth_configure(path = here::here("./credentials.json"))
gm_auth(email = "...@gmail.com")

I guess the cached file just got deleted somehow.

Also, if the oauth app represented by credentials.json is one you want to use, generally, for all or most of your gmailr work, I would store it in a user-level directory and not down in at the project level.

Then I would store this path in the GMAILR_APP env var, as documented in the gmailr README linked above, and call gm_auth_configure() with no arguments, in my scripts.

What you're doing (locating it within the project) makes the most sense if you have multiple apps and/or you are building something to deploy.

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