Skip to content

Instantly share code, notes, and snippets.

@rfaelens
Created July 7, 2020 20:06
Show Gist options
  • Save rfaelens/eb61e6301abf1ab41058d426275b7d42 to your computer and use it in GitHub Desktop.
Save rfaelens/eb61e6301abf1ab41058d426275b7d42 to your computer and use it in GitHub Desktop.
library(shiny)
library(shinyjs) #to easily run javascript
library(shinyBS) #shiny bootstrap; show error dialogs
### Set up a LinkedIn application first
### To do this, go to https://linkedin.com/developers
### Create an application, and set up the redirect URL to your Shiny application
### Go to products and activate Sign in with LinkedIn
ClientID <- "XXXXXXXXXXX" # see the Auth page for your app at https://linkedin.com/developers
ClientSecret <- "XXXXXXXXXX"
RedirectURL <- "https://rfaelens.shinyapps.io/linkedin/"
## See https://docs.microsoft.com/nl-be/linkedin/shared/authentication/authorization-code-flow
ui <- fluidPage(
shinyjs::useShinyjs(),
titlePanel("Linkedin fetch profile demo"),
shinyBS::bsAlert("alert"),
sidebarLayout(
sidebarPanel(
shinyjs::disabled(
actionButton("fetch", "Fetch profile data")
)
),
mainPanel(
verbatimTextOutput("profile")
)
)
)
redirectToLogin <- function(redirect_uri) {
url <- httr::parse_url("https://www.linkedin.com/oauth/v2/authorization")
url$query <- list(
response_type = "code",
client_id = ClientID,
redirect_uri = redirect_uri,
state = "SHOULD_BE_RANDOM",
scope="r_liteprofile"
)
url <- httr::build_url(url)
shinyjs::runjs(paste0("window.location = '", url, "'"))
}
getAccessToken <- function(AuthCode, redirect_uri) {
res <- httr::POST("https://www.linkedin.com/oauth/v2/accessToken",
body=list(
grant_type="authorization_code",
code = AuthCode,
redirect_uri = redirect_uri,
client_id = ClientID,
client_secret = ClientSecret
),
encode = "form")
httr::content(res, as="parsed")
}
getProfile <- function(access_token) {
res <- httr::GET("https://api.linkedin.com/v2/me",
httr::add_headers(`Authorization`=paste0("Bearer ", access_token))
)
httr::content(res, as="parsed")
}
server <- function(input, output, session) {
data <- reactiveValues(auth_code=NULL)
observeEvent(session$clientData, {
query <- parseQueryString(session$clientData$url_search)
### Any time LinkedIn redirects the user back to us, then the 'state' is filled in
if(!is.null(query$state)) {
message("User logged in to LinkedIn")
## This is a response from LinkedIn
if(!is.null(query$error)) {
shinyBS::createAlert(session, "alert", content=query$error_description, title = query$error)
} else {
data$auth_code <- query$code
shinyBS::createAlert(session, "alert", content=query$code, title="Received auth_code")
shinyjs::enable("fetch")
}
} else {
message("User not logged in, redirecting...")
redirectToLogin(RedirectURL)
}
})
observeEvent(input$fetch,{
AuthCode <- data$auth_code
res <- getAccessToken(AuthCode, RedirectURL)
data$profile <- getProfile(res$access_token)
})
output$profile <- renderPrint({
data$profile
})
}
# Run the application
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment