Skip to content

Instantly share code, notes, and snippets.

@nicferrier
Created June 9, 2013 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicferrier/5744866 to your computer and use it in GitHub Desktop.
Save nicferrier/5744866 to your computer and use it in GitHub Desktop.
(defun twaddle-log (con hdr data)
(with-current-buffer (get-buffer-create "*twitter-log*")
(insert (format "%s %S %s\n" con hdr data))))
;; OAuth header and signature implementation
(defun* twaddle|oauth1-header-do (url &key http-params method oauth-token)
"Private function implementing oauth header construction."
(let* ((oauth-params
`(("oauth_consumer_key" . ,twaddle-consumer-key)
("oauth_signature_method" . "HMAC-SHA1")
("oauth_timestamp" . ,(timestamp))
("oauth_nonce" . ,(number-to-string (abs (random))))
("oauth_version" . "1.0")))
(oauth-sign-params
(if oauth-token
(alist "oauth-token" oauth-token oauth-params)
oauth-params))
(sign-params
(append oauth-sign-params
(if (hash-table-p http-params) ; maybe we want cond instead
(kvhash->alist http-params)
http-params)))
(signature-base
(s-format "${method}&${url}&${params}" 'aget
(alist "method" method "url" url
"params" (join (string-sort sign-params) "&"))))
(signing-key
(concat
(url-hexify-string twaddle-consumer-secret)
"&" (when oauth-token
(url-hexify-string oauth-token))))
(oauth-sig (url-hexify-string
(base64-encode-string
(twaddle/hmac-sha1 signing-key signature-base))))
(oauth-sig-params (list2 (format "oauth_signature=%s" oauth-sig)))
(oauth-header (append oauth-sign-params oauth-sig-params)))
(cons "Authorization"
(format "OAuth %s" (join (string-sort oauth-header) ",")))))
(defun* twaddle/oauth1-header (url &key http-params (method "GET") oauth-token)
"Return the value of the OAuth authorization header.
URL is the absolute HTTP url being requested.
HTTP-PARAMS is an alist or hashtable of additional HTTP
parameters, either POST or GET, that are going to be passed in
the request.
METHOD is the HTTP method you will use to send the request,
\"GET\", \"POST\", \"PUT\", \"DELETE\", etc...
OAUTH-TOKEN is an optional OAuth token.
This process is documented here:
http://oauth.net/core/1.0/#signing_process
The implementation of this function is largely provided by
`twaddle|oauth1-header'.
The return value is the string OAuth header."
;; Setup some functions to make the implemetation simpler
(noflet ((string-sort (lst)
(sort lst (lambda (a b)
(string-lessp (car a) (car b)))))
(timestamp () ; pinched from psandford's oauth.el
(format "%d" (ftruncate (float-time (current-time)))))
(join (param-list joiner)
(mapconcat
(lambda (cell)
(format
"%s=%s"
(car cell)
(url-hexify-string (cdr cell))))
param-list joiner))
(alist (&rest conses) ; possible kv function
(loop for cell on conses by 'cddr
if (cdr cell)
collect (cons (car cell) (cadr cell))
else append (car cell)))
(list2 (&rest data) (list data)))
(funcall 'twaddle|oauth1-header-do url
:http-params http-params
:method method
:oauth-token oauth-token)))
(defun twaddle/request ()
(let ((url twaddle-request-token-url))
(web-http-post
(lambda (con hdr data)
(twaddle-log con (format "%S" (kvhash->alist hdr)) data))
:url url
:extra-headers
(list (twaddle/oauth1-header url :method "POST"))
:logging t)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment