Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Small example of Compojure handler working with the LinkedIn API using REST Web Services.

View handler.clj
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
(ns linkedin-clojure.handler
(:use compojure.core)
(:require [compojure.handler :as handler]
[compojure.route :as route]
[hiccup.page :as hpage]
[hiccup.form :as form]
[cheshire.core :as parse]
[ring.util.response :as response]
[clj-http.client :as client]))
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; CONSTANTS
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def ENV "http://localhost:3000")
(def API_KEY "WRITE_YOUR_API_KEY_HERE")
(def SECRET_KEY "WRITE_YOUR_SECRET_KEY_HERE")
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; IN MEMORY
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def lIn-user (atom {:first_name "" :last_name ""}))
 
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; LOGIC
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
(defn- login-view [error]
(hpage/html5
[:div
(if (= error "access_denied")
[:h1 "We feel rejected... sniff sniff"])
(form/form-to [:post (str "https://www.linkedin.com/uas/oauth2/authorization?"
"response_type=code"
"&client_id=" API_KEY
"&scope=r_network%20r_fullprofile%20r_emailaddress"
"&state=DCEEFWF45453sdffef424111234"
"&redirect_uri=" ENV)]
(form/submit-button "Sign-In LinkedIn"))]))
 
(defn- welcome-page
[user]
(str "Hello there buddy, your name is " (user :first_name) " " (user :last_name)) )
 
(defn- request-access-token
"Request the access token and return it."
[authorization_token]
(-> (client/post (str "https://www.linkedin.com/uas/oauth2/accessToken?"
"grant_type=authorization_code"
"&code=" authorization_token
"&redirect_uri=" ENV
"&client_id=" API_KEY
"&client_secret=" SECRET_KEY))
:body
(parse/parse-string)
(get "access_token")))
 
(defn- simple-request
"This is just a simple request to the LinkedIn API,
where we are going to get our profile data."
[access_token]
(let [{:strs [firstName lastName headline siteStandardProfileRequest]}
(-> (client/get (str "https://api.linkedin.com/v1/people/~"
"?oauth2_access_token=" access_token
"&format=json"))
:body
(parse/parse-string))]
(swap! lIn-user #(assoc % :first_name %2 :last_name %3) firstName lastName)
(response/redirect "/welcome")))
 
(defn- login
"Login view, if we get back some query params from
LinkedIn, then we continue request the access token, and publishing
my profile data, otherwise show the LinkedIn Sign In button"
[params]
(if-let [authorization_token (params "code")]
(let [access_token (request-access-token authorization_token)]
(simple-request access_token))
(let [error (params "error")]
(login-view error))))
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; ROUTES
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
 
(defroutes app-routes
(GET "/" {params :query-params} (login params))
(GET "/welcome" [] (welcome-page @lIn-user))
(route/resources "/")
(route/not-found "Not Found"))
 
(def app
(handler/site app-routes))
View handler.clj
1 2 3 4 5 6 7 8 9 10 11 12
(defn- login-view [error]
(hpage/html5
[:div
(if (= error "access_denied")
[:h1 "We feel rejected... sniff sniff"])
(form/form-to [:post (str "https://www.linkedin.com/uas/oauth2/authorization?"
"response_type=code"
"&client_id=" API_KEY
"&scope=r_network%20r_fullprofile%20r_emailaddress"
"&state=DCEEFWF45453sdffef424111234"
"&redirect_uri=" ENV)]
(form/submit-button "Sign-In LinkedIn"))]))
View handler.clj
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
(defn- simple-request
"This is just a simple request to the LinkedIn API,
where we are going to get our profile data."
[access_token]
(let [{:strs [firstName lastName headline siteStandardProfileRequest]}
(-> (client/get (str "https://api.linkedin.com/v1/people/~"
"?oauth2_access_token=" access_token
"&format=json"))
:body
(parse/parse-string))]
(swap! lIn-user #(assoc % :first_name %2 :last_name %3) firstName lastName)
(response/redirect "/welcome")))
 
(defn- login
"Login view, if we get back some query params from
LinkedIn, then we continue request the access token, and publishing
my profile data, otherwise show the LinkedIn Sign In button"
[params]
(if-let [authorization_token (params "code")]
(let [access_token (request-access-token authorization_token)]
(simple-request access_token))
(let [error (params "error")]
(login-view error))))
View handler.clj
1 2 3 4 5 6 7 8 9 10 11 12
(defn- request-access-token
"Request the access token and return it."
[authorization_token]
(-> (client/post (str "https://www.linkedin.com/uas/oauth2/accessToken?"
"grant_type=authorization_code"
"&code=" authorization_token
"&redirect_uri=" ENV
"&client_id=" API_KEY
"&client_secret=" SECRET_KEY))
:body
(parse/parse-string)
(get "access_token")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.