我正在尝试验证用户的 ID 令牌。每当我调用 verifyIdToken 函数时,都会引发以下错误:
java.lang.IllegalArgumentException:必须设置 projectId
这是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 中没有这样的选项