Skip to content

Instantly share code, notes, and snippets.

@siscia
Created November 30, 2012 17:35
Show Gist options
  • Save siscia/4177228 to your computer and use it in GitHub Desktop.
Save siscia/4177228 to your computer and use it in GitHub Desktop.
Login with facebook in clojure
;;; MODELS
(ns morgan.models.social
(:require [clj-http.client :as client])
(:import [org.scribe.oauth OAuthService]
[org.scribe.builder ServiceBuilder]
[org.scribe.model Token Verifier OAuthRequest Verb])
(:import [org.scribe.builder.api FacebookApi TwitterApi]))
(def facebook-service
(-> (doto
(ServiceBuilder.)
(.provider (FacebookApi.))
(.apiKey “your api key”)
(.apiSecret “your api secret”)
(.callback “URL where FB is gonna re-direct your user”))
(.build))) ;;It’s necessary to call .build outside the doto,
;;otherwise we would get a ServiceBuilder and not a OAuthService…
(def facebook-url
(.getAuthorizationUrl facebook-service nil))
(defn get-info-facebook [code]
(let [verifier (Verifier. code)
token (.getAccessToken facebook-service nil verifier)
request (OAuthRequest. (Verb/GET) “https://graph.facebook.com/me”)]
(do
(.signRequest facebook-service token request))
(.getBody (.send request))))
;;; VIEWS
(ns morgan.views.social
(:use [noir.core :only [defpage]]
[noir.response :only [redirect]]
[morgan.models.social]))
(defpage “/facebook” []
(redirect facebook-url))
(defpage “/fb” {:keys [code]}
(get-info-facebook code))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment