A little background: I'm trying to do an API using Clojure that will ask for the user's permission to read his/her emails and then will execute queries to present ordered data.
For this I'm trying to use the Gmail Java API with an existing token.
Using the documentation available, I was able to make a request to get a new token, but then I don't know how to create an instance of com.google.api.services.gmail
using that previously retrieved token.
Here is how my relevant code looks like:
(ns linelos.gmail.core
(:import (com.google.api.client.extensions.jetty.auth.oauth2 LocalServerReceiver)
(com.google.api.client.googleapis.auth.oauth2 GoogleAuthorizationCodeFlow
GoogleClientSecrets
GoogleAuthorizationCodeFlow$Builder)
(com.google.api.client.auth.oauth2 Credential BearerToken TokenResponse)
(com.google.api.client.googleapis.javanet GoogleNetHttpTransport)
(com.google.api.client.http HttpTransport)
(com.google.api.services.gmail Gmail Gmail$Builder)))
(def ^:private http-transport
(GoogleNetHttpTransport/newTrustedTransport))
(def ^:private json-factory (JacksonFactory/getDefaultInstance))
(defn get-connection [access-token]
(let [token-response (-> (TokenResponse.) (.setAccessToken access-token))
credential (-> (Credential. (BearerToken/authorizationHeaderAccessMethod))
(.setFromTokenResponse token-response))
; I'm stuck here
gmail-builder (Gmail$Builder. http-transport json-factory ???)
gmail (-> gmail-builder
(.setApplicationName app-name)
.build)]
gmail))
Am I missing something else, like a helper to create requests using a Token?.
According to their OAuth 2.0 documentation:
GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken);
Plus plus = new Plus.builder(new NetHttpTransport(), JacksonFactory.getDefaultInstance(), credential)
.setApplicationName("Google-PlusSample/1.0")
.build();
The third argument to Gmail$Builder
constructor expects an instance of com.google.api.client.http.HttpRequestInitializer
(or null). Their GoogleCredential
class implements that interface. Have you tried passing credential
as the third argument (where you have ???
)?
Something like this might work:
(defn get-connection [access-token]
(let [token-response (-> (TokenResponse.) (.setAccessToken access-token))
credential (-> (Credential. (BearerToken/authorizationHeaderAccessMethod))
(.setFromTokenResponse token-response))]
(-> (Gmail$Builder. http-transport json-factory credential)
(.setApplicationName app-name)
(.build))))