gpt4 book ai didi

java - 谷歌API : cast credentialBuilder to httprequestInitializer

转载 作者:行者123 更新时间:2023-11-30 02:46:08 29 4
gpt4 key购买 nike

我正在尝试访问 google 分析数据,但在我的 credentialBuilder 上不断收到 ClassCastException。这是我的代码:

(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 cannot be cast to com.google.api.client.http.HttpRequestInitializer

我尝试关注Hello Analytics Reporting API V4: Java quickstart for service accounts例如,我做错了什么?

最佳答案

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)

关于java - 谷歌API : cast credentialBuilder to httprequestInitializer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40153278/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com