r-lib / gmailr

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

gmailr sending email non-iteractively #154

Closed Patrikios closed 1 year ago

Patrikios commented 3 years ago

I fail to use the non-interactive mode for gmailr package. It works when I first, interactively log into and allow the API however after few non-interactive runs it dies off, requesting new access interactively. Below is the debug flow of information from the tool:

email_addr <- "xxx@gmail.com"

options(gargle_verbosity = "debug")
options(gargle_oauth_email = email_addr )
gm_auth_configure( path = path_to_my_token )

email <- gm_mime() %>%
         gm_to(email_addr ) %>%
         gm_from(email_addr ) %>%
         gm_subject( sprintf("Automated eMail on %s", Sys.time() ) %>%
         gm_text_body("test")

gm_send_message(email)

My debug thread looks like follows:

2021-07-01 17:19:28 INFO::Checking for new dates
> trying `token_fetch()`
> trying `credentials_service_account()`
> Error caught by `token_fetch()`:
  Argument 'txt' must be a JSON string, URL or file.
> trying `credentials_app_default()`
> trying `credentials_gce()`
> trying `credentials_byo_oauth()`
> Error caught by `token_fetch()`:
  inherits(token, "Token2.0") ist nicht TRUE
> trying `credentials_user_oauth2()`
> Gargle2.0 initialize
> adding 'userinfo.email' scope
> loading token from the cache
> The gmailr package is requesting access to your Google account
  Select a pre-authorised account or enter '0' to obtain a new token
  Press Esc/Ctrl + C to cancel

1: xxx@gmail.com

Auswahl: 1
> matching token found in the cache
Auto-refreshing stale OAuth token.
> Removing token from the cache:
  C:/Users/xxx/AppData/Local/gargle/gargle/Cache/9bf775756e404caa38fa87f520d83a56_xxx@gmail.com
Fehler in gmailr_POST(c("messages", "send"), user_id, class = "gmail_message",  : 
  Gmail API error: 401
  Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.

I cannot figure what should be the correct workflow.

EDIT: 2021-09-21:

"
R6 Class for sending the emails
"

if(GLOBALS$GMAIL_DEBUG == "ON") options(gargle_verbosity = "debug")

gmail_interface <- R6Class("gmail_interface",
  public = list(
    from = NULL,
    to = NULL,
    initialize = function(){
      logdebug(private$path)
      private$configure()

      self$from <- '1234@gmail.com'
      self$to <- c('xy@gmail.com', 'dew@web.de')

      self$intro()

    },

    intro = function(){
      cat(paste0("Initialized gmail interface with from: ", self$from, " to: ", self$to, ".\n"))
    },

    send = function(subject = sprintf("Automated eMail on %s", Sys.time()), text = "test"){
      email <- gm_mime() %>%
        gm_to(self$to) %>%
        gm_from(self$from) %>%
        gm_subject(subject) %>%
        gm_text_body(text)

      gm_send_message(email)
    }

  ),
  private = list(

    path = file.path(path_current_script, "gmailR_cred"),

    configure = function(){ 

      JSON_FILE = list.files(private$path, pattern = 'json', full.names = TRUE)[1L]
      gm_auth_configure(path = JSON_FILE)

      SECRET = '.secretlocal'  
      gm_auth(email = "1234@gmail.com", 
              scopes = 'send', 
              cache = file.path(
                        private$path,
                        SECRET)
              )

       }
      )
  )

The token is valid for exactly a week. then the gmailr or gargle asks me for a new one interactively. What will work permamently?

jennybc commented 1 year ago

This sounds like the behaviour of tokens obtained with an OAuth client that is in testing mode.

Please see https://github.com/r-lib/gargle/issues/200.