1

I'm running R Studio on an AWS "Ubuntu Server 12.04.2" and accessing R Studio via my browser.

When I try to authenticate google auth API using the package googlesheets with the code: gs_auth(token = NULL, new_user = FALSE, key = getOption("googlesheets.client_id"), secret = getOption("googlesheets.client_secret"), cache = getOption("googlesheets.httr_oauth_cache"), verbose = TRUE)

The problem here is that it redirects me to browser which is of local machine (windows based). Even if I authorize it, it redirects to a URL like "http://localhost:1410/?state=blahblah&code=blahblah".

How do I authorize googlesheets in such case?

I have even tried transfering existing httr-oauth token from my windows machine to remove ubuntu server.

2 Answers2

7

The simplest way to create a gs_auth token from a server is to set the httr_oob_default option to true, which will tell httr to use the out of band method for authenticating. You will be given a URL and expected to return an authorization code.

library(googlesheets)
options(httr_oob_default=TRUE)
gs_auth(new_user = TRUE)
gs_ls()

One thing httr does when you set the httr_oob_default option is to redefine the URI to urn:ietf:wg:oauth:2.0:oob as seen in the code for oauth-init.

Alternatively, you can create a .httr-oauth token manually using httr commands. Use the out of band authentication mode by setting use_oob=TRUE in the oauth2.0_token command.

library(googlesheets)
library(httr)

file.remove('.httr-oauth')

oauth2.0_token(
  endpoint = oauth_endpoints("google"),
  app = oauth_app(
    "google", 
    key = getOption("googlesheets.client_id"), 
    secret = getOption("googlesheets.client_secret")
    ),
  scope = c(
    "https://spreadsheets.google.com/feeds", 
    "https://www.googleapis.com/auth/drive"),
  use_oob = TRUE,
  cache = TRUE
)

gs_ls()

Another, less elegant, solution is to create the .httr-oauth token on your desktop and then copying the file to a server.

nwstephens
  • 71
  • 1
  • 4
  • It really helps me a lot. Thanks! – Daeyoung Kim Aug 22 '16 at 06:45
  • 1
    I've just found this, and it's solved my issues setting up something similar on a remote Debian machine, thanks @user1980554. One thing I noticed is that the oauth file sits in `R`'s working directoty, and you need to make sure you're in that same directory when you run a `googlesheets` action in a script. I decided to `setwd("/")` before creating the oauth file, then adding `setwd("/")` in my scripts, just before any `googlesheets` actions. – rosscova Jan 12 '17 at 22:34
  • 1
    I have been dealing with a similar problem for Google Analytics for so long, that after trying your answer I could not help to shout out loud. Thanks man! – agustin Sep 15 '17 at 07:55
0

After lot of head banging, I found that a package "httpuv" which supports HTTP handling and WebSocket requests from R was creating the problem. It was forcing R to open web browser. Once I uninstalled this package, "googlesheets" gave me a link which I could paste in browser separately and then paste the auth code back in R server.