PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/consumer/twitter.lisp

http://github.com/skypher/cl-oauth
Lisp | 74 lines | 52 code | 17 blank | 5 comment | 1 complexity | 97940b70fdff365335570ab3daf9fdeb MD5 | raw file
Possible License(s): LGPL-3.0
  1. (asdf:oos 'asdf:load-op 'cl-oauth)
  2. (asdf:oos 'asdf:load-op 'hunchentoot)
  3. (defpackage :cl-oauth.twitter-consumer
  4. (:use :cl :cl-oauth))
  5. (in-package :cl-oauth.twitter-consumer)
  6. ;;; insert your credentials and auxiliary information here.
  7. (defparameter *key* "")
  8. (defparameter *secret* "")
  9. (defparameter *callback-uri* "")
  10. (defparameter *callback-port* 8090
  11. "Port to listen on for the callback")
  12. ;;; go
  13. (defparameter *get-request-token-endpoint* "https://api.twitter.com/oauth/request_token")
  14. (defparameter *auth-request-token-endpoint* "https://api.twitter.com/oauth/authorize")
  15. (defparameter *get-access-token-endpoint* "https://api.twitter.com/oauth/access_token")
  16. (defparameter *consumer-token* (make-consumer-token :key *key* :secret *secret*))
  17. (defparameter *request-token* nil)
  18. (defparameter *access-token* nil)
  19. (defun get-access-token ()
  20. (obtain-access-token *get-access-token-endpoint* *request-token*))
  21. ;;; get a request token
  22. (defun get-request-token ()
  23. (obtain-request-token
  24. *get-request-token-endpoint*
  25. *consumer-token*
  26. :callback-uri *callback-uri*))
  27. (setf *request-token* (get-request-token))
  28. (let ((auth-uri (make-authorization-uri *auth-request-token-endpoint* *request-token*)))
  29. (format t "Please authorize the request token at this URI: ~A~%" (puri:uri auth-uri)))
  30. ;;; set up callback uri
  31. (defun callback-dispatcher (request)
  32. (declare (ignorable request))
  33. (unless (cl-ppcre:scan "favicon\.ico$" (hunchentoot:script-name request))
  34. (lambda (&rest args)
  35. (declare (ignore args))
  36. (handler-case
  37. (authorize-request-token-from-request
  38. (lambda (rt-key)
  39. (assert *request-token*)
  40. (unless (equal (url-encode rt-key) (token-key *request-token*))
  41. (warn "Keys differ: ~S / ~S~%" (url-encode rt-key) (token-key *request-token*)))
  42. *request-token*))
  43. (error (c)
  44. (warn "Couldn't verify request token authorization: ~A" c)))
  45. (when (request-token-authorized-p *request-token*)
  46. (format t "Successfully verified request token with key ~S~%" (token-key *request-token*))
  47. (setf *access-token* (get-access-token))
  48. ;; test request:
  49. (babel:octets-to-string
  50. (access-protected-resource "https://api.twitter.com/1.1/search/tweets.json?q=twitter"
  51. *access-token*))))))
  52. (pushnew 'callback-dispatcher hunchentoot:*dispatch-table*)
  53. (defvar *web-server* nil)
  54. (when *web-server*
  55. (hunchentoot:stop *web-server*)
  56. (setf *web-server* nil))
  57. (setf *web-server* (hunchentoot:start (make-instance 'hunchentoot:easy-acceptor :port *callback-port*)))