8

In the R script, when I try to send the email with the following codes below. It asks that 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 abort.

1: email1@gmail.com

without manually entering 1 in the console, how can my R script automatically select my pre-authorised account and sent an email accordingly?

library(gmailr)
gm_auth_configure(path="C:/Users/Google Drive/email.json")

my_email_message <- gm_mime() %>%
  gm_to("email1@gmail.com") %>%
  gm_from("email1@gmail.com") %>%
  gm_subject("My test message") 

gm_send_message(my_email_message)
  • I think you're almost there, you have set the credentials.json file correctly. I think that the call to `gm_auth()` is missing as specified [here](https://github.com/r-lib/gmailr#setup) – anddt Mar 03 '20 at 14:24

2 Answers2

8

This is the unattended / non-interactive authentication problem. I will try to give the rundown of the process as it worked for me - and the problem, exactly like yours, went away. As it states in gmailr/readme - you download json credentials, authenticate once interactively and copy creds to wherever you like. Credentials you can get via python quickstart, or even better - by simply creating a project on https://console.developers.google.com, adding gmail API to it, then creating OAuth credentials for a desktop app. The benefit of the latter approach is you will know exactly where all components are and will be able to repeat as many times as you want. I created a separate google e-mail address for this purpose. You will then download OAuth "client-secret" .json file into your project directory and call it credentials.json (or any other json name you like). Then you will once authenticate interactively running below commands from Rstudio when you are in your project directory:

gm_auth_configure(path  = "credentials.json")
gm_auth(email = TRUE, cache = ".secret")

A webpage will pop up with scary messages, but you will agree to all and from then on you will be using cache. Cache .secret sub-directory that you just created inside your project (and you can give whatever name you wish to the cache directory) is portable - you can copy that alongside your credentials.json over to your shiny-server. It is convenient that all is contained in your project directory. You will need a few lines in your code after that - they should precede the command gm_send_message(your_email_prepared_with_gm_mime) and no more interactive authentication is needed no matter which computer you have copied your project to as long as it has gmailr and gargle (which is a gmailr dependency) installed in R on your server:

gm_auth_configure(path  = "credentials.json")
options(
  gargle_oauth_cache = ".secret",
  gargle_oauth_email = "email_address_used_for_creds@gmail.com"
)
gm_auth(email = "email_address_used_for_creds@gmail.com")

# then compose your e-mail and send it

the last command allows to avoid dialogue for which account to use. This sometimes pops up on first use. gmailr Readme explains it well; my explanation is an encouragement to read it again, if you get stuck. You can read also gmailr reference at https://gmailr.r-lib.org/index.html - it is pretty good. But my guess is - if you have followed the process here you won't even need that.

Note on cache: Default gargle (this is what makes authentication for gmailr happen) cache directory is in some hidden subdirectory of your home directory - so it is specific to you on that computer. However if you set it to be a subdirectory to your R project, the whole OAuth process becomes portable. Just copy your project directory were you want and the OAuth credential pair - the json file and OAuth token(s) in the cache will follow along. Tokens are gzipped binary files that gmail creates cryptographically and deposits in the cache during the "authentication dance". One address paired to one G-project gives one token. One probably could use multiple addresses and google projects in one R project, but I so far have yet to see the need for that.

r0berts
  • 842
  • 1
  • 13
  • 27
  • Hey @r0berts , thanks so much for this answer. Can I lean into the `gm_auth(email = TRUE, cache = ".secret")` command. It appears to be stashing the authentication dance selections. But do you know where this is cached and in what format? – M_Merciless Jul 04 '20 at 06:10
  • 1
    Hi @M_Merciless, it is fairly OK once you read a few times. For `gm_auth` the option `email=TRUE` means look into the cache for a suitable auth token (_a gzipped binary file_). If you say `email=my.address@gmail.com` it means the same, but look for that specific email auth token. Just in case you have a few tokens there. I prefer to give a the specific address even if I use only one id / token. Lastly `cache=.secret` simply gives instruction to cache tokens in your current/project directory `.secret` subdirectory. It will be created if not exists. See also https://gmailr.r-lib.org/index.html – r0berts Jul 04 '20 at 09:07
  • 1
    @r0berts This is a great answer. I read the official docs before this answer. After reading your answer, the official instructions come off as being written as an afterthought (no offense intended to Jim Hester). I think your answer should be edited into the README in some way or another (maybe as an example/tutorial). Thank you :) – The AEB May 29 '21 at 11:51
2

Just add the "from e-mail address" with gm_auth(email = "email1@gmail.com")

library(gmailr)
gm_auth_configure(path="C:/Users/Google Drive/email.json")
gm_auth(email = "email1@gmail.com")

my_email_message <- gm_mime() %>%
  gm_to("email1@gmail.com") %>%
  gm_from("email1@gmail.com") %>%
  gm_subject("My test message") 

gm_send_message(my_email_message)```
Warender
  • 23
  • 3