我正在尝试将具有oauth授权的shiny应用程序部署到shinyapps.io。我创建了一个简单的模板应用程序用于说明(见下文)。我能够部署并启动该应用程序。但是,登录按钮没有任何作用,什么也没有发生。没有显示任何消息(甚至没有显示“身份验证失败”。)。
日志显示该应用程序正在“等待浏览器中的身份验证...”和“请将您的浏览器指向以下网址:...”当我手动输入提供的网址时,身份验证提供商的网站将打开,我登录,并且正在被重定向到应用程序(在新选项卡中),但话又说回来,没有任何反应。
谢谢您的帮助。
library(shiny)
library(httr)
# OAuth application details
korap_app <- oauth_app("korap-client", key = "tmNm9RMf3hnFMpnTN4NbFT", secret = NULL, redirect_uri = "https://danieljach.shinyapps.io/KED_interface/")
korap_endpoint <- oauth_endpoint(NULL, authorize = "settings/oauth/authorize", access = "api/v1.0/oauth2/token", base_url = "https://korap.ids-mannheim.de")
# UI
ui <- fluidPage(
titlePanel("OAuth Shiny App Template"),
mainPanel(
actionButton("loginButton", "Login with OAuth"),
textOutput("authStatus")
)
)
# Server
server <- function(input, output, session) {
# Reactive value to store authentication status
authenticated <- reactiveVal(FALSE)
# OAuth login event
observeEvent(input$loginButton, {
# Open the browser for user authentication
token <- oauth2.0_token(korap_endpoint, korap_app, scope = "search match_info", cache = FALSE)
# Check if authentication is successful
if (!is.null(token)) {
authenticated(TRUE)
output$authStatus <- renderText("Authentication Successful!")
} else {
output$authStatus <- renderText("Authentication Failed.")
}
})
# Display content based on authentication status
observe({
if (authenticated()) {
# Display authenticated content here
} else {
# Display content for non-authenticated users
}
})
}
# Run the app
shinyApp(ui, server)
此处提供了根本问题的描述和解决方法:
感谢 RKorAPClient 开发人员kupietz。
这是工作代码。
library(shiny)
library(RKorAPClient)
# UI
ui <- fluidPage(
titlePanel("Login Shiny App Template"),
mainPanel(
textInput("accessToken", "Please enter your access token here."),
actionButton("loginButton", "Enter."),
textOutput("authStatus"),
textInput("userQuery", "Please enter a search word."),
actionButton("queryGo", "Search."),
tableOutput("queryResults")
)
)
# Server
server <- function(input, output, session) {
# Reactive value to store authentication status
authenticated <- reactiveVal(FALSE)
rv <- reactiveValues(kco = NULL)
# login event
observeEvent(input$loginButton, {
# use user token to open connection
rv$kco <- new("KorAPConnection", accessToken = input$accessToken)
# Check if connection is successful
if (!is.null(reactive(rv$kco))) {
authenticated(TRUE)
output$authStatus <- renderText("Authentication Successful!")
} else {
output$authStatus <- renderText("Authentication Failed.")
}
})
# query event
observeEvent(input$queryGo, {
output$queryResults <- renderTable(corpusQuery(rv$kco, input$userQuery, context = '3', metadataOnly = FALSE) %>%
fetchNext() %>%
slot('collectedMatches'))
})
}
# Run the app
shinyApp(ui, server)