Firebase Java 管理 SDK 错误
Firebase Java admin SDK error
我正在尝试验证用户的 ID 令牌。每当我调用 verifyIdToken 函数时,都会抛出以下错误:
java.lang.IllegalArgumentException: projectId must be set
这是 servlet 代码:
public class GeneratorInfoServlet extends HttpServlet {
FirebaseApp app = null;
@Override
public void init(ServletConfig config) {
InputStream serviceAccount = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("/WEB-INF/service_key.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
.setDatabaseUrl("https://project-id.firebaseio.com/")
.build();
this.app = FirebaseApp.initializeApp(options);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter out = resp.getWriter();
StringBuffer tokenBuffer = new StringBuffer();
String line;
try {
BufferedReader reader = req.getReader();
while ((line = reader.readLine()) != null)
tokenBuffer.append(line);
this.verifyToken(tokenBuffer.toString());
out.print("{'success': false}");
} catch (Exception e) {
System.err.print(e);
out.print("{'success': true}");
}
}
private void verifyToken(String idToken) {
System.out.println(app);
FirebaseAuth.getInstance(this.app).verifyIdToken(idToken)
.addOnSuccessListener(decodedToken -> {
String uid = decodedToken.getUid();
System.out.println(uid);
}).addOnFailureListener(e -> System.out.println(e));
}
}
我一直在浏览文档,但没有找到直接设置项目 ID 的方法。但是我认为首先没有必要,因为它已经写在 service_key 和数据库 url.
中
谢谢!
编辑:
常规 Java SDK(非管理员)在 FirebaseOptions 构建器中内置了 setApplicationId("PROJECT_ID") 函数。但是 Admin SDK 中没有这样的选项
我初始化我的 Firebase 应用有点不同,请看我的代码
InputStream in = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("/WEB-INF/service_key.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setServiceAccount(in)
.setDatabaseUrl(".....")
.build();
这适用于 Admin SDK 版本 4.0.4
我正在尝试验证用户的 ID 令牌。每当我调用 verifyIdToken 函数时,都会抛出以下错误:
java.lang.IllegalArgumentException: projectId must be set
这是 servlet 代码:
public class GeneratorInfoServlet extends HttpServlet {
FirebaseApp app = null;
@Override
public void init(ServletConfig config) {
InputStream serviceAccount = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("/WEB-INF/service_key.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
.setDatabaseUrl("https://project-id.firebaseio.com/")
.build();
this.app = FirebaseApp.initializeApp(options);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter out = resp.getWriter();
StringBuffer tokenBuffer = new StringBuffer();
String line;
try {
BufferedReader reader = req.getReader();
while ((line = reader.readLine()) != null)
tokenBuffer.append(line);
this.verifyToken(tokenBuffer.toString());
out.print("{'success': false}");
} catch (Exception e) {
System.err.print(e);
out.print("{'success': true}");
}
}
private void verifyToken(String idToken) {
System.out.println(app);
FirebaseAuth.getInstance(this.app).verifyIdToken(idToken)
.addOnSuccessListener(decodedToken -> {
String uid = decodedToken.getUid();
System.out.println(uid);
}).addOnFailureListener(e -> System.out.println(e));
}
}
我一直在浏览文档,但没有找到直接设置项目 ID 的方法。但是我认为首先没有必要,因为它已经写在 service_key 和数据库 url.
中谢谢!
编辑:
常规 Java SDK(非管理员)在 FirebaseOptions 构建器中内置了 setApplicationId("PROJECT_ID") 函数。但是 Admin SDK 中没有这样的选项
我初始化我的 Firebase 应用有点不同,请看我的代码
InputStream in = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("/WEB-INF/service_key.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setServiceAccount(in)
.setDatabaseUrl(".....")
.build();
这适用于 Admin SDK 版本 4.0.4