Advertisement
Guest User

nth auth code in clojure

a guest
Mar 18th, 2010
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 2.52 KB | None | 0 0
  1. ;;;
  2. ;;; auth.clj
  3. ;;;
  4. ;;; Perform various miracles involving OAuth authentication.
  5. ;;;
  6. ;;; Looks for a serialized oauth object.  If it finds one, returns
  7. ;;; an object of type Twitter to the caller.  If it doesn't find one,
  8. ;;; it sends the user off to twitter for a pin, accepts the pin,
  9. ;;; serializes the object, and returns Twitter to the caller (I think).
  10. ;;;
  11. ;;; Fails catastrophically if you screw up the pin.  Checks that the pin
  12. ;;; is seven digits (which might not be carved in stone).
  13. ;;;
  14. ;;; Written and maintained by Stephen Ramsay
  15. ;;;
  16. ;;; Last Modified: Wed Mar 17 23:49:56 CDT 2010
  17.  
  18. (ns gropius.sramsay.nth
  19.   (:import
  20.      (twitter4j Twitter)
  21.      (twitter4j TwitterFactory)
  22.      (twitter4j.http RequestToken)
  23.      (twitter4j.http AccessToken)
  24.  
  25.      (java.io File)
  26.      (java.io InputStream)
  27.      (java.io BufferedReader)
  28.      (java.io FileOutputStream)
  29.      (java.io FileInputStream)
  30.      (java.io ObjectOutputStream)
  31.      (java.io ObjectInputStream)
  32.      (java.io InputStreamReader)))
  33.  
  34. (System/setProperty "twitter4j.oauth.consumerKey" "SECRET STUFF")
  35. (System/setProperty "twitter4j.oauth.consumerSecret" "MORE SECRET STUFF")
  36.  
  37. (def twitter (.getInstance (new TwitterFactory)))
  38.  
  39. ;;
  40. ;; Magical serialization functions
  41. ;;
  42.  
  43. (defn serialize [object filename]
  44.   (let [os (new FileOutputStream filename)]
  45.     (with-open [oo (new ObjectOutputStream os)]
  46.       (.writeObject oo object))))
  47.  
  48. (defn deserialize [filename]
  49.   (let [is (new FileInputStream filename)]
  50.     (with-open [oi (new ObjectInputStream is)]
  51.       (cast twitter4j.http.AccessToken (.readObject oi)))))
  52.  
  53. ;;
  54. ;; Sort out authentication
  55. ;;
  56.  
  57. (defn get-auth []
  58.   (if (.exists (new File "oauth.ser")) ; If we have a serialized OAuth object
  59.     (.setOAuthAccessToken twitter (deserialize "oauth.ser")) ; Go with that.
  60.     (do ; Otherwise, give the user a URL and have them give you the pin
  61.       (def request_token (.getOAuthRequestToken twitter))
  62.       (println "Open the following URL to grant this program access to your account.")
  63.       (println (.getAuthorizationURL request_token))
  64.       (println "After doing that, you may enter your PIN: ")
  65.       (def pin (read-line))
  66.       (if (re-matches #"[0-9]{7}" pin) ; 7-digit number, right?
  67.         (do
  68.           (serialize (.getOAuthAccessToken twitter request_token pin) "oauth.ser")
  69.         (do
  70.           (println "That pin doesn't seem to be correct.") ; Should do something
  71.           (System/exit 0))))))                             ; nicer here
  72.   twitter) ; Return the twitter object
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement