问题描述
当尝试在Java应用程序上使用firebase admin时,我收到此错误:
Exception in thread "main" com.google.cloud.firestore.FirestoreException: java.util.UnkNownFormatConversionException: Conversion = 'F'
at com.google.cloud.firestore.FirestoreException.networkException(FirestoreException.java:89)
at com.google.cloud.firestore.FirestoreOptions$DefaultFirestoreRpcFactory.create(FirestoreOptions.java:92)
at com.google.cloud.firestore.FirestoreOptions$DefaultFirestoreRpcFactory.create(FirestoreOptions.java:82)
at com.google.cloud.ServiceOptions.getRpc(ServiceOptions.java:561)
at com.google.cloud.firestore.FirestoreOptions.getFirestoreRpc(FirestoreOptions.java:385)
at com.google.cloud.firestore.FirestoreImpl.<init>(FirestoreImpl.java:67)
at com.google.cloud.firestore.FirestoreOptions$DefaultFirestoreFactory.create(FirestoreOptions.java:73)
at com.google.cloud.firestore.FirestoreOptions$DefaultFirestoreFactory.create(FirestoreOptions.java:66)
at com.google.cloud.ServiceOptions.getService(ServiceOptions.java:541)
at firebase.Firebase.initialize(Firebase.java:30)
at launch.Main.main(Main.java:33)
Caused by: java.io.IOException: java.util.UnkNownFormatConversionException: Conversion = 'F'
at com.google.cloud.firestore.spi.v1.GrpcFirestoreRpc.<init>(GrpcFirestoreRpc.java:137)
at com.google.cloud.firestore.FirestoreOptions$DefaultFirestoreRpcFactory.create(FirestoreOptions.java:90)
... 9 more
Caused by: java.util.UnkNownFormatConversionException: Conversion = 'F'
at java.base/java.util.Formatter$FormatSpecifier.conversion(Formatter.java:2839)
at java.base/java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2865)
at java.base/java.util.Formatter.parse(Formatter.java:2713)
at java.base/java.util.Formatter.format(Formatter.java:2655)
at java.base/java.util.Formatter.format(Formatter.java:2609)
at java.base/java.lang.String.format(String.java:3292)
at com.google.api.pathtemplate.ValidationException.message(ValidationException.java:91)
at com.google.api.pathtemplate.ValidationException.<init>(ValidationException.java:86)
at com.google.api.pathtemplate.PathTemplate.encodeUrl(PathTemplate.java:1093)
at com.google.api.pathtemplate.PathTemplate.instantiate(PathTemplate.java:775)
at com.google.api.pathtemplate.PathTemplate.instantiate(PathTemplate.java:702)
at com.google.api.pathtemplate.PathTemplate.instantiate(PathTemplate.java:711)
at com.google.firestore.v1.DatabaseRootName.toString(DatabaseRootName.java:122)
at com.google.cloud.firestore.spi.v1.GrpcFirestoreRpc.<init>(GrpcFirestoreRpc.java:116)
... 10 more
供参考,以下是来源:
package firebase;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.FirestoreOptions;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaSEOptions;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class Firebase {
private static boolean initialized = false;
static Firestore db;
static public void initialize() throws IOException {
if(initialized)
return;
FirestoreOptions options = FirestoreOptions.getDefaultInstance().toBuilder()
.setCredentials(GoogleCredentials.fromStream(new ByteArrayInputStream(System.getenv("firebase").getBytes(StandardCharsets.UTF_8))))
.build();
System.out.println(options.getCredentials());
System.out.println(System.getenv("firebase"));
db = options.getService();
initialized = true;
return;
}
}
System.getenv("firebase")
打印以下内容:
{ "type": "service_account","project_id": "<PROJECT_ID>","private_key_id": "<PRIVATE_KEY_ID>","private_key": "-----BEGIN PRIVATE KEY----- xxxxxx -----END PRIVATE KEY-----\n","client_email": "xxxxxxxxx","client_id": "xxxxxxxxxxxxx","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/firebase-adminsdk-s16j9%40xxxxxxx.iam.gserviceaccount.com" }
即使使用不明显的版本,我也只能找到一次%
字符:在4
之前的末尾。
options.getCredentials()
打印出看似正确的数据,因此我认为问题与访问Firebase时出错有关。
我已经尝试替换firebase密钥。谢谢您的帮助。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)