我试图访问谷歌分析数据,但不断得到ClassCastException在我的credentialBuilder。下面是我的代码:
(defn credential
[]
(doto (GoogleCredential$Builder.)
(.setTransport http-transport)
(.setJsonFactory json-factory)
(.setServiceAccountId "X")
(.setServiceAccountPrivateKeyFromP12File (File. "X"))
(.setServiceAccountScopes (AnalyticsReportingScopes/ANALYTICS_READONLY))
(.build)))
(defn analytics
[]
(doto (AnalyticsReporting$Builder. http-transport json-factory (credential))
(.setApplicationName "X")
(.build)))
我得到的确切错误是:
com.google.api.client.googleapis.auth.oauth2。GoogleCredential$Builder不能强制转换为com.google.api.client.http.HttpRequestInitializer
我尝试遵循Hello分析报告API V4: Java快速启动服务帐户的例子,我做错了什么?
doto
返回作为其第一个参数传递的对象-在您的情况下,它将返回GoogleCredential$Builder
的实例并忽略(.build)
调用的结果。您需要将其更改为将调用的结果返回给(.build)
:
(defn credential
[]
(->
(doto (GoogleCredential$Builder.)
(.setTransport http-transport)
(.setJsonFactory json-factory)
(.setServiceAccountId "X")
(.setServiceAccountPrivateKeyFromP12File (File. "X"))
(.setServiceAccountScopes (AnalyticsReportingScopes/ANALYTICS_READONLY)))
(.build))
doto
被宏展开为如下形式:
(doto (StringBuilder.)
(.append "a")
(.append "b"))
是
(let [obj (StringBuilder.)]
(.append obj "a")
(.append obj "b")
obj)