No Title When I finaly Take it upon myself to write this is what happens!!!

Clojure Java Inter-op and Google Drive Api - Part I

One of the biggest advantages of Clojure claimed by its community is its interoperability with java. I decided to take take them on there word and test it out with Google drive api, and see how flexible and native it feels.

We will convert a simple program given on the main documentation page of Google Drive api page to a Clojure program. First we will try to keep it as same as there implementation having least amount of changes. Later we will try to make it more Clojure like, by using its native libraries and using Google libraries to a minimum.

You will have to do the basic setup form this page. May be even give a quick look a the java implementation, because we will be following it quite faithfully.


First you should create a new lein project:

$ lein new sample-drive app

This will create a set of files and some basic stuff. Now you will have to add some dependencies add required libraries to the project.clj file. You should be able to find this file at the root of the new project lein created for you. Change it to look some thing like this.

(defproject simple-drive "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url ""
  :license {:name "Eclipse Public License"
            :url ""}
  :dependencies [[org.clojure/clojure "1.5.1"]
                 [ "1.15.0-rc"]
                 [ "1.15.0-rc"]
                 [ "v2-rev79-1.15.0-rc"]]
  :main simple-drive.core)

google-api-services-drive is the the only library explicitly mentioned on the download page. But the other (google-oauth-client, google-http-client-jackson) are dependencies. You could use third party libraries that provide similar functionality like, clj-oauth2.

Another interesting not is we are getting them from maven repositories, which are essentially cpan or pypy like function for java. And the fact that we are even able to leverage that from Clojure is amazing.


The program itself is a single function in the given sample, but will split it up slightly so that we can keep the main program in a single let statement. Much of the code is java directly converted to clojure.

(ns simple-drive.core
(import '(
          GoogleAuthorizationCodeFlow GoogleCredential
          GoogleTokenResponse GoogleAuthorizationCodeFlow$Builder)
        '( FileContent HttpTransport)
        '( NetHttpTransport)
        '( JsonFactory)
        '( JacksonFactory)
        '( Drive DriveScopes Drive$Builder)
        '( File))
(require '

;; you have to get your own from google as mentioned in the help page.

(def REDIRECT_URI "urn:ietf:wg:oauth:2.0:oob")

(defn get-auth-code [url]
  (println "Please open the following URL in your browser "
           "then type the authorization code:")
  (println (str "  " url))

(defn insert-file []
  (let [body (new File)]
    (doto body
      (.setTitle "Readme")
      (.setDescription "A test document")
      (.setMimeType "text/plain"))

(defn do-the-stuff
  (let [httpTransport (new NetHttpTransport)
        jsonFactory (new JacksonFactory)
        flow (-> (new GoogleAuthorizationCodeFlow$Builder httpTransport
                      jsonFactory CLIENT_ID CLIENT_SECRET
                      (list (. DriveScopes DRIVE)))
                 (.setAccessType "online")
                 (.setApprovalPrompt "auto")
        url (-> (.newAuthorizationUrl flow)
                (.setRedirectUri REDIRECT_URI)
        code (get-auth-code url)
        response (-> (.newTokenRequest flow code)
                     (.setRedirectUri REDIRECT_URI)
        credential (-> (new GoogleCredential)
                       (.setFromTokenResponse response))
        ;; Create a new authorized API client
        service (-> (new Drive$Builder httpTransport
                         jsonFactory credential) (.build))
        body (insert-file)
        fileContent ( "")
        mediaContent (new FileContent "text/plain" fileContent)
        file (-> (.files service)
                 (.insert body mediaContent)
    (println "File ID:" (.getId file))))

(defn -main
  "This is the equvalent of public static void main."
  [& args]
  ;; work around dangerous default behaviour in Clojure
  (alter-var-root #'*read-eval* (constantly false))

Some of the stuff you might note, are that we have to explicitly import all the java inner classes. In java these are available to you if you just import the containing class. The whole program is written in a imperative manner, you set a whole bunch of variables, in between you do a few things. Instead of (new classname &args) we could have just said (classname. &args).