Unable to confirm Google credentials for `Gmailr` when running via Windows Task Scheduler

Cross-posting from StackOverflow: https://stackoverflow.com/questions/75514060/unable-to-confirm-google-credentials-for-gmailr-when-running-via-windows-task

I'm trying to schedule an R script to run through Windows Task Scheduler, and I'd like this script to send an automated email through gmailr when it has finished running. Currently, I have the task scheduled to run in Task Scheduler, and it will run and save output into a folder. The file path for the output directory contains the run date, so I know that the script is running as scheduled. The problem is that the email isn't sent after the output is saved.

To troubleshoot, I've run the script manually in RStudio, and it runs successfully. The output is saved, and the email will be sent through gmailr. This makes me think the code is ok and that I have some sort of higher-level authentication issue when running a non-interactive session.

When I look at Task Scheduler, the "Last Run Result" for the task is "Incorrect function. (0x800700001)." I've saved the error log when running this script through Task Scheduler, and it contains:

"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"

After systematically attempting to run the script through Task Scheduler when certain lines of code are missing, the error message appears to be due to gm_auth(cache = here(".secret"), email = "[SENDER_EMAIL]")

I've included a reprex of my code below.

here::i_am("[PROJ_DIR]/test-automated-email.R")

library(tidyverse)
library(here)
library(glue)
library(gmailr)
library(gargle)

options(gargle_verbosity = "debug")

pass_body <- glue("Everything worked great!")

gm_auth_configure(path = here("[MY_JSON_FILE]"))
gm_auth(cache = here(".secret"), email = "[SENDER_EMAIL]")

email <- gm_mime() %>%
  gm_from("[SENDER_EMAIL]") %>%
  gm_to("[RECIPIENT_EMAIL]") %>%
  gm_subject("Success") %>%
  gm_html_body(pass_body)
gm_send_message(email)

The output from Gargle debug is

trying `token_fetch()`
trying `credentials_service_account()`
Error caught by `token_fetch()`:
Argument 'txt' must be a JSON string, URL or file.
trying `credentials_external_account()`
aws.ec2metadata not installed; can't detect whether running on EC2 instance
trying `credentials_app_default()`
trying `credentials_gce()`
trying `credentials_byo_oauth()`
Error caught by `token_fetch()`:
inherits(token, "Token2.0") is not TRUE
trying `credentials_user_oauth2()`
Gargle2.0 initialize
adding "userinfo.email" scope
loading token from the cache
no matching token in the cache
initiating new token
Error caught by `token_fetch()`:
OAuth2 flow requires an interactive session.
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

For whatever reason, it seems like the token can't be found when initiating the script through Task Scheduler.

Some other (hopefully helpful) info:

  • I have the same issue when I hard code the working directory and avoid using the here package's functions
  • I've saved the working directory and the contents of the working directory to a log file when running via Task Scheduler, and the cache and .json files are present. This makes me think I'm pointing to the correct path in gm_auth_configure() and gm_auth().
  • The .json file is an OAuth file
  • I've tried as many possible combinations of the parameters for gm_auth() and gm_auth_configure() that I can think of (e.g., gm_auth_configure(path = "[MY_JSON_FILE]")), but I still have the same issue regardless of the combination.

Does anyone have any thoughts on how to resolve this? Any and all help is appreciated!

Thanks!

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.