Code Monkey home page Code Monkey logo

accord's Introduction

Accord

A simple OAuth 1.0/a, 2.0 consumer client for Clojure built on top of clj-http.

Warning

This is a work-in-progress as well as my first foray into Clojure...use at your own risk.

Usage

Using Accord is easy and straight forward:

;; We'll be using a popular OAuth 2.0 provider for this example
(require '[accord.oauth2 :as oauth2])

;; You don't have to use Cheshire, but we'll parse the JSON with it in this
;; example
(require '[cheshire.core :refer [parse-string]])

;; Get a real client id and secret from:
;;
;;  https://developers.facebook.com/apps
;;
(def client-id "440483442642551")
(def client-secret "cd54f1ace848fa2a7ac89a31ed9c1b61")

(def authorize-url "https://graph.facebook.com/oauth/authorize")
(def access-token-url "https://graph.facebook.com/oauth/access_token")
(def redirect-uri "https://www.facebook.com/connect/login_success.html")

Now we have some basic constants we can use to seed our client with.

(def fb (oauth2/service client-id
                        client-secret
                        authorize-url
                        access-token-url
                        :base-url "https://graph.facebook.com/"))

Great! At this point we should construct an authorization URL and ask our user to visit it.

(oauth2/make-authorize-url fb {:scope "read_stream"
                               :response_type "code"
                               :redirect_uri redirect-uri})
; => "https://graph.facebook.com/oauth/authorize?client_id=..."

Typically the redirect URI would direct the user back to your application. On this redirect we would find a code parameter, assuming the user authorized our application. Once we obtain that code parameter's value we can get an access token.

(oauth2/get-access-token fb {:query-params {:code code
                                            :redirect_uri redirect-uri}})

Because the fb var is a ref, calling this function updates the proper access-token field associated with the ref. From this point we can make authenticated requests with the provider. In other words, we have an accord with the provider.

(require '[accord.http :as http])  ;; require our HTTP client wrapper
(def user (parse-string (:body (http/get fb "me")) true))
(println "currently logged in as: " (:name user))
; => "currently logged in as: Foo Bar"

License

Copyright © 2013 Max Countryman

Distributed under the BSD License.

accord's People

Contributors

maxcountryman avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

jartai

accord's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.