There is an example in the readme, that you can see working as a Shiny app here
If you are intending it for just logging in purposes, check out GoogleID package which is built with googleAuthR
with this in mind.
Example code below:
## in global.R
library(googleAuthR)
library(shiny)
options(googleAuthR.scopes.selected = "https://www.googleapis.com/auth/urlshortener")
options(googleAnalyticsR.webapp.client_id = "YOUR_PROJECT_KEY")
options(googleAnalyticsR.webapp.client_secret = "YOUR_CLIENT_SECRET")
shorten_url <- function(url){
body = list(
longUrl = url
)
f <- gar_api_generator("https://www.googleapis.com/urlshortener/v1/url",
"POST",
data_parse_function = function(x) x$id)
f(the_body = body)
}
## server.R
source("global.R")
server <- function(input, output, session){
## Create access token and render login button
access_token <- callModule(googleAuth, "loginButton")
short_url_output <- eventReactive(input$submit, {
## wrap existing function with_shiny
## pass the reactive token in shiny_access_token
## pass other named arguments
with_shiny(f = shorten_url,
shiny_access_token = access_token(),
url=input$url)
})
output$short_url <- renderText({
short_url_output()
})
}
## ui.R
ui <- fluidPage(
googleAuthUI("loginButton"),
textInput("url", "Enter URL"),
actionButton("submit", "Shorten URL"),
textOutput("short_url")
)
### If the above global.R, server.R and ui.R files are in folder "test" like so:
## /home
## |->/test/
## /global.R
## /ui.R
## /server.R
##
## Port 1221 has been set in your Google Project options as the port to listen to
## as explained in authentication setup section
## run below in /home directory
shiny::runApp("./test/", launch.browser=T, port=1221)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…