Linkedin sign-in functionality using Clojure

Knoldus Inc.
Knoldus - Technical Insights
2 min readApr 8, 2014

This blog will show you how to add Linkedin sign in functionality in your clojure web application.

First Create an application on Linkedin by following the link

https://developer.linkedin.com/documents/quick-start-guide Fill all the information and provide the Redirect URI which you use in your application .Once you create the application you will get the API Key and Secret Key.

Let’s reference the [noir.response :as resp] [cheshire.core :as parse] [clj-http.client :as client][noir.session :as session] in your app.routes.linkedin.clj namespace

[code language=”html”]

(ns app.routes.linkedin
(:use compojure.core)
(:require [noir.response :as resp]
[cheshire.core :as parse]
[clj-http.client :as client]
[noir.session :as session]))

[/code]

In app.routes.linkedin.clj define an atom linkedin-atom to store the information

[code language=”html”]
(def linkedin-atom (atom {:firstName “” :lastName “” :linkedin-id “”}))

[/code]

Now define the routes :-
The GET linked-routes route simply call the linkedin function

[code language=”html”]
(defroutes linkedin-routes
(GET “/linkedin” []
(linkedin)))

[/code]

[code language=”html”]

(defn- linkedin []
(resp/redirect (str “https://www.linkedin.com/uas/oauth2/authorization?"
“response_type=your-secret-code”
“&client_id=” your-API-key
“&scope=r_fullprofile%20r_emailaddress”
“&state=DCEEFWF45453sdffef424111234”
“&redirect_uri=” your redirect uri)))

[/code]

Now we are going to use the authorization code to get the access token which gives us access to the user information through the REST API. In order to get the access token, we need to issue a POST request like the one below:

[code language=”html”]

(defn- request-access-token [authorization_token]
(get (parse/parse-string (:body
(client/post
(str “https://www.linkedin.com/uas/oauth2/accessToken?"
“grant_type=authorization-code”
“&code=” authorization-token
“&redirect_uri=” Your redirect URI
“&client_id=” your-API-key
“&client_secret=” SECRET-KEY)))) “access_token”))

[/code]

After a linkedin user grant access to the app, we’ll get a response containing access token on the redirection URI we provided while setting up our Linkedin app we’ll use “compojure.core’s” GET to get the response:

[code language=”html”]

(GET “/auth_linkedin” {params :query-params} (linkedin-login params))

[/code]

[code language=”html”]

(defn- linkedin-login [params]
(if-let [authorization_token (params “code”)]
(let [access_token (request-access-token authorization_token)]
(profile-details-request access_token))
(let [error (params “error”)]
(resp/redirect “/register”))))

[/code]

As we can see in the linkedin-login, if there is an authorization token, we proceed by getting the access_token, and finally make a formal request to LinkedIn.

--

--

Knoldus Inc.
Knoldus - Technical Insights

Group of smart Engineers with a Product mindset who partner with your business to drive competitive advantage | www.knoldus.com