PageRenderTime 4ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/consumer/google.lisp

http://github.com/skypher/cl-oauth
Lisp | 80 lines | 56 code | 18 blank | 6 comment | 1 complexity | 4e6255467807ba723c41d966e90ea511 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.google-consumer
  4. (:use :cl :cl-oauth))
  5. (in-package :cl-oauth.google-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://www.google.com/accounts/OAuthGetRequestToken")
  14. (defparameter *auth-request-token-endpoint* "https://www.google.com/accounts/OAuthAuthorizeToken")
  15. (defparameter *get-access-token-endpoint* "https://www.google.com/accounts/OAuthGetAccessToken")
  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 (scope)
  23. ;; TODO: scope could be a list.
  24. (obtain-request-token
  25. *get-request-token-endpoint*
  26. *consumer-token*
  27. :callback-uri *callback-uri*
  28. :user-parameters `(("scope" . ,scope))))
  29. (setf *request-token* (get-request-token "http://www.google.com/calendar/feeds/"))
  30. (let ((auth-uri (make-authorization-uri *auth-request-token-endpoint* *request-token*)))
  31. (format t "Please authorize the request token at this URI: ~A~%" (puri:uri auth-uri)))
  32. ;;; set up callback uri
  33. (defun callback-dispatcher (request)
  34. (declare (ignorable request))
  35. (unless (cl-ppcre:scan "favicon\.ico$" (hunchentoot:script-name request))
  36. (lambda (&rest args)
  37. (declare (ignore args))
  38. (handler-case
  39. (authorize-request-token-from-request
  40. (lambda (rt-key)
  41. (assert *request-token*)
  42. (unless (equal (url-encode rt-key) (token-key *request-token*))
  43. (warn "Keys differ: ~S / ~S~%" (url-encode rt-key) (token-key *request-token*)))
  44. *request-token*))
  45. (error (c)
  46. (warn "Couldn't verify request token authorization: ~A" c)))
  47. (when (request-token-authorized-p *request-token*)
  48. (format t "Successfully verified request token with key ~S~%" (token-key *request-token*))
  49. (setf *access-token* (get-access-token))
  50. ;; test request:
  51. (let ((result (access-protected-resource
  52. "http://www.google.com/calendar/feeds/default/allcalendars/full?orderby=starttime"
  53. *access-token*)))
  54. (if (stringp result)
  55. result
  56. (babel:octets-to-string result)))))))
  57. (pushnew 'callback-dispatcher hunchentoot:*dispatch-table*)
  58. (defvar *web-server* nil)
  59. (when *web-server*
  60. (hunchentoot:stop *web-server*)
  61. (setf *web-server* nil))
  62. (setf *web-server* (hunchentoot:start (make-instance 'hunchentoot:acceptor :port *callback-port*)))