Created
November 30, 2012 17:33
-
-
Save siscia/4177219 to your computer and use it in GitHub Desktop.
Login with facebook in clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; 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)))) | |
;;;;;;;VIEW | |
(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