PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms 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
 1
 2(asdf:oos 'asdf:load-op 'cl-oauth)
 3(asdf:oos 'asdf:load-op 'hunchentoot)
 4
 5(defpackage :cl-oauth.google-consumer
 6  (:use :cl :cl-oauth))
 7
 8(in-package :cl-oauth.google-consumer)
 9
10;;; insert your credentials and auxiliary information here.
11(defparameter *key* "")
12(defparameter *secret* "")
13(defparameter *callback-uri* "")
14(defparameter *callback-port* 8090
15  "Port to listen on for the callback")
16
17
18;;; go
19(defparameter *get-request-token-endpoint* "https://www.google.com/accounts/OAuthGetRequestToken")
20(defparameter *auth-request-token-endpoint* "https://www.google.com/accounts/OAuthAuthorizeToken")
21(defparameter *get-access-token-endpoint* "https://www.google.com/accounts/OAuthGetAccessToken")
22(defparameter *consumer-token* (make-consumer-token :key *key* :secret *secret*))
23(defparameter *request-token* nil)
24(defparameter *access-token* nil)
25
26(defun get-access-token ()
27  (obtain-access-token *get-access-token-endpoint* *request-token*))
28
29;;; get a request token
30(defun get-request-token (scope)
31  ;; TODO: scope could be a list.
32  (obtain-request-token
33    *get-request-token-endpoint*
34    *consumer-token*
35    :callback-uri *callback-uri*
36    :user-parameters `(("scope" . ,scope))))
37
38(setf *request-token* (get-request-token "http://www.google.com/calendar/feeds/"))
39
40(let ((auth-uri (make-authorization-uri *auth-request-token-endpoint* *request-token*)))
41  (format t "Please authorize the request token at this URI: ~A~%" (puri:uri auth-uri)))
42
43
44;;; set up callback uri
45(defun callback-dispatcher (request)
46  (declare (ignorable request))
47  (unless (cl-ppcre:scan  "favicon\.ico$" (hunchentoot:script-name request))
48    (lambda (&rest args)
49      (declare (ignore args))
50      (handler-case
51          (authorize-request-token-from-request
52            (lambda (rt-key)
53              (assert *request-token*)
54              (unless (equal (url-encode rt-key) (token-key *request-token*))
55                (warn "Keys differ: ~S / ~S~%" (url-encode rt-key) (token-key *request-token*)))
56              *request-token*))
57        (error (c)
58          (warn "Couldn't verify request token authorization: ~A" c)))
59      (when (request-token-authorized-p *request-token*)
60        (format t "Successfully verified request token with key ~S~%" (token-key *request-token*))
61        (setf *access-token* (get-access-token))
62        ;; test request:
63        (let ((result (access-protected-resource
64                        "http://www.google.com/calendar/feeds/default/allcalendars/full?orderby=starttime"
65                        *access-token*)))
66          (if (stringp result)
67            result
68            (babel:octets-to-string result)))))))
69
70(pushnew 'callback-dispatcher hunchentoot:*dispatch-table*)
71
72
73(defvar *web-server* nil)
74
75(when *web-server*
76  (hunchentoot:stop *web-server*)
77  (setf *web-server* nil))
78
79(setf *web-server* (hunchentoot:start (make-instance 'hunchentoot:acceptor :port *callback-port*)))
80