一点背景:我正在尝试使用Clojure执行API,该API会要求用户允许阅读他/她的电子邮件,然后执行查询以显示有序数据。
为此,我尝试将Gmail Java API与现有令牌一起使用。
使用documentation available,我能够发出获取新令牌的请求,但后来我不知道如何使用之前的com.google.api.services.gmail
创建实例检索到的令牌。
以下是我的相关代码的样子:
(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))
我是否遗漏了其他内容,比如使用令牌创建请求的助手?
答案 0 :(得分:1)
根据他们的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();
Gmail$Builder
构造函数的第三个参数需要com.google.api.client.http.HttpRequestInitializer
(或null)的实例。他们的GoogleCredential
类实现该接口。您是否尝试将credential
作为第三个参数(您有???
)?
这样的事可能有用:
(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))))