在没有浏览器的情况下在AWS Ubuntu上验证Google表格

Pra*_*ena 1 ubuntu r ubuntu-12.04 httr r-googlesheets

我正在AWS"Ubuntu Server 12.04.2"上运行R Studio并通过我的浏览器访问R Studio.

当我尝试使用包googlesheets和代码验证google auth API时: 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)

这里的问题是它将我重定向到本地机器(基于Windows)的浏览器.即使我授权它,它也会重定向到" http:// localhost:1410 /?state = blahblah&code = blahblah " 这样的网址.

在这种情况下如何授权googlesheets?

我甚至尝试从我的Windows机器上传输现有的httr-oauth令牌来删除ubuntu服务器.

nws*_*ens 7

gs_auth从服务器创建令牌的最简单方法是将该httr_oob_default选项设置为true,这将告诉httr使用带外方法进行身份验证.您将获得一个URL,并希望返回授权码.

library(googlesheets)
options(httr_oob_default=TRUE)
gs_auth(new_user = TRUE)
gs_ls()
Run Code Online (Sandbox Code Playgroud)

设置httr_oob_default选项时httr做的一件事就是重新定义URI,urn:ietf:wg:oauth:2.0:ooboauth-init的代码所示.

或者,您可以.httr-oauth使用httr命令手动创建令牌.通过use_oob=TRUEoauth2.0_token命令中进行设置,使用带外认证模式.

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()
Run Code Online (Sandbox Code Playgroud)

另一个不太优雅的解决方案是.httr-oauth在桌面上创建令牌,然后将文件复制到服务器.