Authenticate to Dropbox API with newLISP

Q&A's, tips, howto's
Locked
hilti
Posts: 140
Joined: Sun Apr 19, 2009 10:09 pm
Location: Hannover, Germany
Contact:

Authenticate to Dropbox API with newLISP

Post by hilti »

Hi newLISPers

here comes a quick hack for the weekend. I needed a way to authenticate against the Dropbox API for one of my projects. Unfortunately the API uses oAuth and needs "https" so I have to use the CURL library instead of newLISP build-in (get-url).

At first You need to create Your own Dropbox API keys. That's very easy.
Now You need this little script to get the authentication tokens.

Code: Select all

#!/usr/bin/env newlisp

;; BEGIN SETTINGS

;; Set Your own Dropbox API keys
;; You can create these keys here: https://www.dropbox.com/developers/apps
(set 'oauth-consumer-key "this-is-your-app-key") ;; this is the App key
(set 'oauth-signature "this-is-so-secret") ;; this is the App secret
(set 'curl "curl") ;; set path to curl executable

;; END SETTINGS


(set 'post-request (append {'Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="} oauth-consumer-key {", oauth_signature="} oauth-signature {&"'}))
(set 'post-target "https://api.dropbox.com/1/oauth/request_token")

;; GET TOKEN SECRET
(set 'curlpost (append curl " --silent --header " post-request " " post-target))
(set 'response (first (exec curlpost)))
(set 'oauth_token_secret  (first (parse response "&")))
(set 'the-secret (last (parse oauth_token_secret "=")))
(set 'oauth_token (last (parse response "&")))

;; AUTH BY USER
(set 'authurl "https://www.dropbox.com/1/oauth/authorize?")
(set 'callbackurl "&oauth_callback=http://localhost/auth_complete")
(set 'curlauth (append curl " --silent " authurl oauth_token callbackurl))
(exec curlauth)
(println "Opening browser for authorization ... please follow further steps in Your browser.")
(set 'launchcmd (append "open " authurl oauth_token callbackurl))
(exec launchcmd)

(println "When authorization on Dropbox.com was successful press the 'a' key: ")
(while (!= (set 'key (read-key)) 97) 
	(println "When authorization on Dropbox.com was successful press the 'a' key: ")
)

;; GET ACCESS TOKEN
(set 'access-request (append {'Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="4n1wccbr1cyx3la", } oauth_token {", oauth_signature="xppxoa9rpqoviwc&} the-secret {"'}))

(set 'access-target "https://api.dropbox.com/1/oauth/access_token")
(set 'curlaccess (append curl " --silent --header " access-request " " access-target))
(set 'access_response (first (exec curlaccess)))
(println "Access response (JSON): " access_response)

(set 'access_token (parse access_response "&"))
(println "Access token: " access_token)
(set 'oauth_token (last (parse (nth 1 access_token) "=")))
(println "OAuth token: " oauth_token)
(write-file "oauth-token.txt" (string oauth_token))

(set 'access_token_secret (last (parse (first (parse access_response "&")) "=")))
(println "Access token secret: " access_token_secret)
(write-file "token.txt" (string access_token_secret))
(exit)
---- LAUNCH the script ---- :-)

Beware: I've just tested it on OSX.

When You run this script in OSX terminal it'll automatically open Your default browser and asks if newLISP get's access to Your Dropbox. Grant access now.

Now go back to terminal and press the "a" key to go on with the script. We need to pause, because we've to wait for user interaction (grant access) in the browser.

Finally You should see the Dropbox API response with OAuth Token and Access Token Secret.

----- Use the Dropbox API ----- :-) :-)

With these tokens/keys we can use the Dropbox API. Here's a little snippet to get the account information.

Code: Select all

#!/usr/bin/env newlisp

;; BEGIN SETTINGS

;; Set Your own Dropbox API keys
;; You can create these keys here: https://www.dropbox.com/developers/apps
(set 'oauth-consumer-key "this-is-your-app-key") ;; this is the App key
(set 'oauth-signature "this-is-so-secret") ;; this is the App secret
(set 'curl "curl") ;; set path to curl executable

;; END SETTINGS

(set 'access_token_secret (read-file "token.txt"))
(set 'oauth_token (read-file "oauth-token.txt"))
(set 'header (append {'Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="} oauth-consumer-key {", oauth_token="} oauth_token {", oauth_signature="} oauth-signature {&} access_token_secret {"'}))

(define (json2list response)
	(json-parse (first response))	
)

(define (accountinfo)
	(set 'endpoint "https://api.dropbox.com/1/account/info")
	(set 'url (append curl " --silent --header " header " " endpoint))	
	(set 'response (exec url))
	(json2list response)
)

(println (accountinfo))
(exit)
Hopefully everything works and You have some fun with the Dropbox API. When I find some time, maybe I'll write a complete Dropbox module.

Cheers
Marc
--()o Dragonfly web framework for newLISP
http://dragonfly.apptruck.de

rickyboy
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: Authenticate to Dropbox API with newLISP

Post by rickyboy »

Cool! Thanks, Marc!
(λx. x x) (λx. x x)

Locked