尝试向 Firestore 验证 Java 客户端时出现“凭据无法获取元数据”错误

2023-12-20

我把我的东西放在一起thought是你好世界火库Java 客户端身份验证,但它总是死掉

警告:您的应用程序已使用 Google Cloud SDK 中的最终用户凭据进行身份验证。我们建议大多数服务器应用程序使用服务帐户。如果您的应用程序继续使用 Cloud SDK 中的最终用户凭据,您可能会收到“超出配额”或“API 未启用”错误。有关服务帐户的更多信息,请参阅https://cloud.google.com/docs/authentication/ https://cloud.google.com/docs/authentication/。 凭证:ServiceAccountCredentials{clientId=104986335035620796437,[电子邮件受保护] /cdn-cgi/l/email-protection、 privateKeyId=8e...11、transportFactoryClassName=com.google.auth.oauth2.OAuth2Utils$DefaultHttpTransportFactory、tokenServerUri=https://oauth2.googleapis.com/token https://oauth2.googleapis.com/token,范围=[],serviceAccountUser=null} 线程“主”中的异常 java.util.concurrent.ExecutionException:com.google.api.gax.rpc.UnavailableException:io.grpc.StatusRuntimeException:不可用:凭证无法获取元数据

“凭据无法获取元数据”无法解释 - 我的服务帐户 JSON 是否错误?为什么它仍然给我那个警告,它们有关联吗?

val firestoreOptions = FirestoreOptions.getDefaultInstance().toBuilder()
        .setProjectId("nextbot3")
        .setCredentials(GoogleCredentials.fromStream(ClassLoader.getSystemClassLoader().getResourceAsStream("serviceAccountKey.json")))
        .setDatabaseId("nextbot3")
        .setTimestampsInSnapshotsEnabled(true)
        .build()
println("Credentials: " + firestoreOptions.credentials)
val db = firestoreOptions.service!!

val docRef = db.collection("users").document("a-user").collection("devices").document("a-bot")
println(docRef.set(mapOf("hello" to "world")).get())

我的 serviceAccountKey.json 直接来自我从云(不是 firebase)项目管理页面下载的内容:

{
  "type": "service_account",
  "project_id": "nextbot3",
  "private_key_id": "8edf2b2607309e5da929109550090a5818cd8511",
  "private_key": "-----BEGIN PRIVATE KEY--...--END PRIVATE KEY-----\n",
  "client_email": "[email protected] /cdn-cgi/l/email-protection",
  "client_id": "104986335035620796437",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/junkbot%40nextbot3.iam.gserviceaccount.com"
}

当我们创建FirestoreOptions通过默认实例(即FirestoreOptions.getDefaultInstance().toBuilder()),我们得到一个配置有应用程序默认凭证的构建器。这些凭据优先于您指定的服务帐户。使用FirestoreOptions.newBuilder()以避免这种情况。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尝试向 Firestore 验证 Java 客户端时出现“凭据无法获取元数据”错误 的相关文章

随机推荐