使用 Rstudio 服务器时在 httr 中配置 listener_endpoint

RIn*_*atM 5 google-analytics r rstudio httr rstudio-server

我正在努力使用httroauth2.0 功能连接到 Google Analytics

oauth2.0_token(oauth_endpoints("google")
  , oauth_app("google", client.id, client.secret)
  , scope = "https://www.googleapis.com/auth/analytics.readonly")
Run Code Online (Sandbox Code Playgroud)

它在我本地的 Rstudio 中运行良好,但在基于 AWS 的 Rstudio Server 中会中断。当我同意在浏览器中传递数据并且谷歌将我重定向到页面 http://localhost:1410/?state=codehere 时出现错误

在本地 Rstudio 中启动身份验证时,浏览器响应消息 -身份验证完成。请关闭此页面并返回 R,以防 Rstudio 服务器它只是此网页不可用

我怀疑我需要更改listener_endpoint配置,但是如何更改?我应该输入我的 Rstudio 服务器地址而不是默认的 127.0.0.1 吗?或者是httr+Rtudio服务器的缺陷,我不应该打扰?

小智 1

您的重定向 URI 是问题的一部分。Httr 的 oauth2.0_token() 函数识别正确的。当您设置项目时,Google Analytics 创建了两个重定向 URI,一种可在 RStudio IDE(本地)上使用,另一种可在 RStudio 基于 Web 的环境中用于带外身份验证:“urn :ietf:wg:oauth:2.0:oob"

一旦您通过身份验证,以下代码应该可以工作。

library(httr)
ga_id         <- YourProjectID
client_id     <- YourClientID
redirect_uri  <- 'urn:ietf:wg:oauth:2.0:oob'
scope         <- YourScope 
client_secret <- YourSecret
response_type <-'code'

auth1 <- oauth2.0_token(
   endpoint = oauth_endpoints("google"),
   app = oauth_app(
      "google", 
      key = client_id, 
      secret = client_secret
      ),
   scope,
   use_oob = TRUE,
   cache = TRUE
)
Run Code Online (Sandbox Code Playgroud)

——安