【问题标题】:Configuring listener_endpoint in httr when using Rstudio server使用 Rstudio 服务器时在 httr 中配置 listener_endpoint
【发布时间】:2015-12-01 16:21:10
【问题描述】:

我正在努力使用httr oauth2.0 函数连接到 Google Analytics

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

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

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

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

【问题讨论】:

  • 在本地进行。将令牌保存为 r 数据文件。将其上传到 AWS 并使用缓存的令牌。
  • 这正是我现在所做的,但我认为其他人也会对更优雅的解决方案感兴趣

标签: r google-analytics rstudio httr rstudio-server


【解决方案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
)

-- 安

【讨论】:

    【解决方案2】:

    您可以使用带外身份验证 -

    options(httr_oob_default = TRUE)
    

    【讨论】:

      猜你喜欢
      • 2012-04-06
      • 2015-10-26
      • 2019-02-17
      • 2020-09-23
      • 1970-01-01
      • 2022-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多