r-lib / gmailr

Access the Gmail RESTful API from R.
https://gmailr.r-lib.org
Other
229 stars 56 forks source link

Cannot run new gmailr version with Rscript #121

Closed vinaybugz closed 4 years ago

vinaybugz commented 5 years ago

I have a cronjob that sends out emails using gmailr and rscript. This was working fine till gmailr updated to the newer version. I have set up the identity using gm_auth_configure, and using .httr-oauth and gargle_oauth_cache, gargle_oauth_email. These all work fine if I am running the job in Rstudio, and the emails fire through (without me having to interact with gmail login, etc). However, when I try to run the same file through rscript in the same directory, I keep getting the message:


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

I have simplified the code to the bare minimum (identities hidden for obvious reasons):

httr::set_config(httr::config(http_version=0))
library(gmailr)
gm_auth_configure(path="csecret_sender.json")
options(
  gargle_oauth_cache=".secret",
  gargle_oauth_email="sender@gmail.com"
)

email_text<-
  gm_mime() %>%
  gm_to("user1@gmail.com") %>%
  gm_cc("sender@gmail.com") %>%
  gm_from("sender@gmail.com") %>%
  gm_subject("Daily Run Report from ProdServer") %>%
  gm_html_body(paste0("Dear Admin", "This is a test"))

gm_send_message(email_text)

If I bring in gm_auth(email="sender@gmail.com"), the Rstudio version stops running and gives the same message as the Rscript version. In fact, the rstudio version works fine and sends emails if I don't include gm_auth() in any form, and crashes with the same error message if gm_auth() is added to the code.

I have the .httr-oauth file in the same folder, and have .secret folder as well as the json file. I followed instructions as per:gargle's noninteractive auth, GmailR instructions, as well as the issue-solvers indicated in Issue 115 and community_issue

What am I missing?

Rozenn commented 4 years ago

HI, I have also the same issue using gmailr in a shinyapps.io application. I have also followed the instructions, in particular the issue-solvers indicated in issue 115, and I keep having the same message :

Error: Can't get Google credentials. Are you running gmailr in a non-interactive session? Consider:

-> I have deployed with my .json file AND .secret files as described in the issue 115...

Please, an help would be very useful !!!

jimhester commented 4 years ago

There is some documentation this at https://github.com/r-lib/gmailr#using-gmailr-in-deployed-applications, and also some more generic gargle documentation on handling auth non-interactively at https://gargle.r-lib.org/articles/non-interactive-auth.html#provide-an-oauth-token-directly