OAuth2.0 with R/Shiny and Databricks

Filip Akkad - Senior Software Developer

Filip Akkad

I am going to write a blog post about how to integrate RStudio workbench with Databricks using oauth2.0.

TL;DR

  1. Install Databricks CLI
  2. Register custom application in Databricks
  3. Initialize Shiny application
  4. Authenticate with Databricks using OAuth2.0
  5. Use Databricks API to fetch data from Databricks workspace
  6. Display data in Shiny application

What is Databrics?

Databricks is a company founded by the original creators of Apache Spark. Databricks grew out of the AMPlab project at UC Berkeley, which was involved in making Apache Spark, an open-source distributed computing framework built atop Scala and Apache Mesos.

Why to authenthicate with OAuth in Databricks? In simple terms, comparing to PAT

Databricks supports two types of authentication: OAuth 2.0 and Personal Access Token (PAT). OAuth 2.0 is the recommended authentication method. OAuth 2.0 is an industry-standard protocol for authorization. It enables third-party applications to obtain limited access to HTTP services, either on behalf of a resource owner or by allowing a third-party application to obtain access on its own behalf. OAuth 2.0 is a more secure method of authentication than PAT because it enables you to control the level of access that third-party applications have to your Databricks resources. OAuth 2.0 also enables you to revoke access to your Databricks resources at any time.

Pre-requisites

  1. R/Shiny
  2. Python (will be used by reticulate package)
  3. Access to Databricks account + running workspace

Are you ready? Let’s go!

Register custom application in Databricks

Initialize Shiny application

app.r
library(shiny)
 
ui <- fluidPage(
  textOutput("link"),
  uiOutput("pdf")
)
 
server <- function(input, output, session) {
  example_get_data_url <- session$registerDataObj(
    name = "example-get-api",
    data = list(),
    filterFunc = function(data, req) {
      if(req$REQUEST_METHOD == "GET") {
        pdfPath = './assets/sample.pdf'
        content = readBin(pdfPath, "raw", file.info(pdfPath)$size)
        httpResponse(200, "application/pdf", content)
      }
    }
  )
  output$pdf <- renderUI(
    tags$iframe(
      src = example_get_data_url
    )
  )
 
  output$link <- renderText({
    paste0(
      session$clientData$url_protocol,
      '//',
      session$clientData$url_hostname,
      ':',
      session$clientData$url_port,
      '/',
      example_get_data_url
    )
  })
}
 
shinyApp(ui, server)

Comments