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