Authenticating google sheets on AWS Ubuntu without browser

筅森魡賤 提交于 2019-12-03 21:30:20

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!