Linkedin sign-in functionality using Clojure


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


(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]))

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

(def linkedin-atom (atom {:firstName "" :lastName "" :linkedin-id ""}))

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

  (defroutes linkedin-routes
  (GET "/linkedin" []
  (linkedin)))


(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)))

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:


(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"))

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:


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


(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"))))

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.

This entry was posted in Clojure. Bookmark the permalink.

One Response to Linkedin sign-in functionality using Clojure

  1. Pingback: Links & reads for 2014 Week 15 | Martin's Weekly Curations

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s