Skip to content

Commit

Permalink
Add r shiny oauth test app
Browse files Browse the repository at this point in the history
  • Loading branch information
dbkegley committed Jul 30, 2024
1 parent e4e2e94 commit 0b3faa3
Show file tree
Hide file tree
Showing 2 changed files with 1,052 additions and 0 deletions.
34 changes: 34 additions & 0 deletions local/r-shiny-oauth-app/app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
library(httr2)
library(shiny)

ui <- fluidPage(textOutput("access_token"))

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

# read the user-session-token header
user_session_token <- session$request$HTTP_POSIT_CONNECT_USER_SESSION_TOKEN

output$access_token <- renderText({
# construct a token exchange request
server_url <- Sys.getenv("CONNECT_SERVER")
api_key <- Sys.getenv("CONNECT_API_KEY")
url <- paste0(server_url, "__api__/v1/oauth/integrations/credentials")
body <- list(
grant_type = "urn:ietf:params:oauth:grant-type:token-exchange",
subject_token_type = "urn:posit:connect:user-session-token",
subject_token = user_session_token
)

# fetch the viewer's OAuth Access Token
resp <- httr2::request(url) |>
httr2::req_headers(Authorization = paste("Key", api_key)) |>
httr2::req_body_form(!!!body) |>
req_error(is_error = \(resp) FALSE) |>
httr2::req_perform()

httr2::resp_body_json(resp)
})
}

# start the Shiny app
shinyApp(ui = ui, server = server)
Loading

0 comments on commit 0b3faa3

Please sign in to comment.