【问题标题】:How to use (re/)CAPTCHA with R/Shiny?如何在 R/Shiny 中使用 (re/)CAPTCHA?
【发布时间】:2017-05-31 13:47:26
【问题描述】:

假设您在https://lalaland.shinyapps.io/mail-form/ 上有一个简单的应用程序,用作联系表单。您想在发送电子邮件之前测试发件人是否不是机器人。这可能类似于:

全球

library(shiny)
library(shinyAce)
library(mailR)

ui.R

ui<-shinyUI(
      fluidPage(  

              fluidRow(
                    column(2
                           ,textInput("contact_name", "Name*", placeholder = "Ed Snow") 
                    ),
                    column(2, offset = 0
                           ,textInput("contact_email", "Email*", placeholder = "eddie@lubyanka.com")
                    )
              ),
              fluidRow(
                    column(4,
                           aceEditor(outputId = "contact_message", value = "...", fontSize = 13)
                    )
              ),
              fluidRow(
                    column(2,
                           checkboxInput("contact_not_a_robot", "I'm not a robot*", value = FALSE), # !!! <---
                           actionButton("contact_click_send", "Send")
                           ))
      )

)

server.R

server <- shinyServer(function(session,input, output) {

      observeEvent(input$contact_click_send, {

            if( is.null(input$contact_click_send) || input$contact_click_send==0 
                || !input$contact_not_a_robot){ # !!! <---
                  return(NULL)
            }

            send.mail(from = "kremlin@gmail.com",
                      to = "trumptower@gmail.com",
                      subject = "Shower time!",
                      body = input$contact_message,
                      smtp = list(host.name = "smtp.gmail.com"
                                  , port = 465
                                  , user.name = "kremlin@gmail.com"
                                  , passwd = "DONALD_BIG_HANDS123"
                                  , ssl = TRUE),
                      authenticate = TRUE,
                      html = TRUE, send = TRUE)


            # reset form
            updateTextInput(session, "contact_name",  value = "")
            updateTextInput(session, "contact_email", value = "")
            updateAceEditor(session, "contact_message", value = "Message sent succesfully!")
            updateCheckboxInput(session, "contact_not_a_robot", value = FALSE)
            updateActionButton(session, "contact_click_send", icon = icon("check"))
      })

})

问题换了一种方式:如何将 (re/)CAPTCHA 编入此 R/Shiny 联系表单?

【问题讨论】:

  • GitHub 上有可用的软件包来执行此操作。 reCAPTCHA v2 - github.com/carlganz/shinyCAPTCHA reCAPTCHA v3 - github.com/sarthi2395/shinygCAPTCHAv3
  • @SiddharthArthi 太好了,我去看看。 CRAN 上有什么东西吗?还有一些分步指南/文档?
  • 恐怕我们在 CRAN 中没有为此提供包。您打算使用哪个版本的 reCAPTCHA?我可以据此提供帮助。
  • @SiddharthArthi 将它放在 CRAN 上当然是值得鼓励的。非常欢迎为 v3 提供完整、详细、分步的答案。

标签: r shiny captcha recaptcha


【解决方案1】:

对于 reCAPTCHAv3,请使用 https://github.com/sarthi2395/shinygCAPTCHAv3

devtools::install_github("sarthi2395/shinygCAPTCHAv3")

将此添加到您的 global.R

library(shinygCAPTCHAv3)

ui.R

ui<-shinyUI(
      fluidPage(  
               GreCAPTCHAv3Ui(<your site key>,"homepage","responseReceived"),

               #Rest of your code
               )
           )

您必须在其中输入您在 reCAPTCHA 管理控制台中为您注册的域生成的 SiteKey。在这里,我已将 action 指定为“主页”,但您可以选择适合您目的的操作 (https://developers.google.com/recaptcha/docs/v3)。

responseReceived 是我们将在此处使用的对象,用于将收到的令牌传递给闪亮的服务器,以便与 Google 进行验证。

server.R

server <- shinyServer(function(session,input, output) {

      observeEvent(input$responseReceived, {
                   result <- GreCAPTCHAv3Server(<your secret key>,input$responseReceived)

                  if(result$success){

                     #Rest of your server logic.

                     }
            })

})

您必须在此处输入您在 reCAPTCHA 管理控制台中为您注册的域生成的 SecretKey

如果一切顺利,当您启动网页时,您会在右下角看到 reCAPTCHA 框。希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 2022-01-06
    • 1970-01-01
    • 2017-03-18
    • 2020-12-15
    • 1970-01-01
    • 2021-12-24
    相关资源
    最近更新 更多