Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use the new gmailr version in shinyapps.io #115

Closed
nicocriscuolo opened this issue Aug 14, 2019 · 13 comments
Closed

Unable to use the new gmailr version in shinyapps.io #115

nicocriscuolo opened this issue Aug 14, 2019 · 13 comments

Comments

@nicocriscuolo
Copy link

Hello and thanks a lot for this useful package!

I have some issues with the new version of gmailr. I followed the steps of the README and I have created a project on the Google Cloud Platform. I have downloaded the .json file and, locally, I have configured my account through the gm_auth_configure(), specifying my key ID and my secret. A popup allowed me to connect my Gmail account with my application. Then, I have used the function gm_auth(email = "my-email") to authorise every time just that e-mail to send messages from the app.

Now in my server I have:

  • gm_auth_configure with the path to the .json file;
  • gm_auth with my e-mail.

If I launch the app locally it works, but every time I deploy it on shinyapp.io (including the .json file) it freezes waiting for a redirection of the browser.

By the way, after a launched my app locally, the .httr-oauth file was created in my project folder, but at this point I don't really know if I need to deploy also this file.

Could you please give me any suggestion to set up the right code to make the functions working also online?

Thanks!

@jimhester
Copy link
Member

You need to deploy the oauth file on the server as well, this is the same as the previous versions of gmailr

@nicocriscuolo
Copy link
Author

Thank you very much for the answer Jim.

Actually I have deployed also the .httr-oauth file online but the screen remains white, waiting for the browser authentication. When I created the .httr-oauth file launching the shinyapp for the first time (with the above mentioned functions at the beginning of my server) I was working locally, and the file automatically was inserted in the working directory of my app.

Now if, locally, I restart the session and remove only the gm_auth_configure function, after launching the app in the new session (always locally) I get the following error 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.

That's way I think that for working locally I always also need to specify the path of my .json file through the gm_auth_configure function.

As concern the shinyapp deployed online, even though the two functions are present at the beginning of the server, inside my logs I get the following error message:

Screenshot 2019-08-14 at 21 12 12

I am wondering if the .httr-oauth works just locally and maybe the issue is determined by something that I should set directly on the Google Cloud Platform.

What do you think?

Thank you so much for your availability.

@nicocriscuolo
Copy link
Author

nicocriscuolo commented Aug 15, 2019

Jim,

I don't know if it would be a solution, but maybe I have understood the source of the problem. My shiny app uses also a token generated from the package rdrop2 to upload files in dropbox, and the .httr-oauth file has the same name of the one generated every time with gmailr.

That's way my token is overwritten and the application can't get the credentials. By your knowledge, do you think it's possible to rename the .httr-oauth file generated with gmailr or producing it directly with a different name?

Best,

Nico

@jimhester
Copy link
Member

See the documentation in gargle with how to test the directory where the oauth tokens are stored https://gargle.r-lib.org/articles/non-interactive-auth.html#create-a-cache-for-a-specific-project, the short answer is you can use options(gargle_oauth_cache = ".secrets") to set the location to wherever you would like for a specific project.

@nicocriscuolo
Copy link
Author

I tested the token created after having removed the one for the rdrop2 package, but it still doesn't work online. Is there some guidelines of the specific operations to do for setting up gmailr specifically for an online shinyapp? For example, with the last version in my code I had just the functions for e-mail composition and send message, the .httr-oauth file was stored in my project folder and everything was ok, but now I don't know if the initial passages for the configuration, done through gm_auth_configure and gm_auth are right or not. For example, I don't understand while I keep obtaining the message "Google Credentials not found" if at the beginning of my server there's the function "gm_auth(email = "authorized_email", path = "MY_JSON.json").

Thank you again for your help!

@sanjmeh
Copy link

sanjmeh commented Aug 20, 2019

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 get the same errors (above) while running locally too. The gm_auth() fails even though the auth token is lying in the current working directory. There is no redirection to a browser like earlier version used to do.

I also tried
gm_auth(token = ".httr-oauth") or
gm_auth(path = "mass_mailer.json")
But situation does not improve. It keeps saying:

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

What could be wrong?

@jimhester
Copy link
Member

You need to call gm_auth_configure() to setup the oauth app before calling gm_auth().

@sanjmeh
Copy link

sanjmeh commented Aug 20, 2019

You need to call gm_auth_configure() to setup the oauth app before calling gm_auth().

It worked. Sorry for jumping the gun. Have a nice evening!

@jimhester
Copy link
Member

Good to hear, in 86627e9 I made the error message more clear when you call gm_auth() before calling gm_auth_configure(). I also added some more documentation to the readme.

@nicocriscuolo
Copy link
Author

nicocriscuolo commented Aug 21, 2019

Hi @jimhester and @sanjmeh, thanks for your clarification. I've followed the procedure inside the README.Rmd, but I still the application doesn't work online, but just locally. Here's a list of what I did:

  1. after downloading the "credentials.json" in my working directory ("App") I called, at the beginning of my server.R, the functions gm_auth_configure(path = "credentials.json") and gm_auth() by simply starting my shiny app locally;

  2. I got redirected to a new page and so I configured my gmail account;

  3. everytime I run my app locally, at the beginning of my server I have this to functions: gm_auth_configure() and gm_auth(email = "my_email@gmail.com"), and the emails are sent correctly;
    3.1. ps: this time, after configuration, no .httr-oauth file has been "automatically" created. When I send an e-mail through the app that is running locally, in the R console i can see the message "Auto-refreshing stale OAuth token".

  4. Now the ONLINE part on shinyapps.io:

4.1. if I deploy my shinyapp with the functions gm_auth_configure() and gm_auth(email = "my_email@gmail.com") at the beginning of the server (before server <- function..) the app freezes at the start because there is no path for the .json file;
4.1. if I deploy the app by specifying gm_auth_configure("credentials.json") and gm_auth(email = "my_email@gmail.com") the app waits my authentication in browser before starting, and in my Logs, on shinyapps.io, I get always the same error (see attached image).

Again, the procedure to make it working locally works, but I still can't make it work online. Is there a step that I actually can't see?

Screenshot 2019-08-21 at 10 38 52

Thanks again!

@jimhester
Copy link
Member

You need to copy both the credentials.json and your oauth token cache (e.g. in .secrets to the server)

@nicocriscuolo
Copy link
Author

nicocriscuolo commented Aug 22, 2019

Jim, thanks. It's working now. For FUTURE REFERENCES, I am going to write all the workflow that I used to correctly set up the gmailr functions to make a shiny App working on shinyapps.io, hope it can help someone else:

  1. refer to the gmailr README to download the .json file. It's important that in the creation phase of the .json key you "Desktop App";
  2. store this file in a subfolder of your .Rproj (e. g. "~/data/credentials/MY-JSON.json";
  3. in the Google Cloud Console, configure the Oauth screen by specifying the authorised users to configure the app (you, so enter the gmail address with which you want to configure the app);
  4. at the beginning of your server.R file (before server <- function(input, ..)) insert the functions:
  • options(gargle_oauth_cache = ".secrets");
  • gargle::gargle_oauth_cache();
  • gm_auth_configure(path = "data/credentials/MY-JSON.json");
  • gm_auth();
  1. launch your shiny App locally, a popup window will appear to configure your gmail address. When configure, remember to give the application permission to read/edit/send emails. After the configuration, in the main folder of your project the hidden folder ".secrets" will appear. This folder contains a token with alpha-numeric characters and your address at the end (e. g. sdasdjabq2eqw232342323GMAIL-ADDRESS@GMAIL.COM);
  2. make sure that the ".secrets" folder is in the first folder of your .Rproj, the same in which you store the ui.R and server.R;
  3. again, at the beginning of the server.R remove the first two functions written above and just leave:
  • gm_auth_configure(path = "data/credentials/MY-JSON.json");
  • gm_auth(email = "CONFIGURED-EMAIL@gmail.com", cache = ".secrets");
  1. deploy your shiny App online, making sure that inside the deployment folder also the alpha-numeric token is selected.

Best,

Nico

@nicocriscuolo
Copy link
Author

Hi Jim,

I have another small question related to this topic. Is it possible the file that has to be placed in the .secrets folder (sdasdjabq2eqw232342323GMAIL-ADDRESS@GMAIL.COM) expires after some time or there's the need to re-do the authorisation process? I'm asking this because few days ago, in my local folders, the folder .secrets was empty and I was unable to send emails from my app.

Thanks,

Nico

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants