我正在尝试使用 Firebase Admin SDK(java) 从服务器端实现验证令牌 ID,但出现错误:
无法解析服务帐号:必须设置“project_id”
我在 firebase 的以下路径中生成了我的凭据:
项目设置->服务帐户并生成一个新的私钥,我有这个:
{
"type": "service_account",
"project_id": "apilogintest-9c5f5",
"private_key_id": "<privateKeyId...>",
"private_key": "-----BEGIN PRIVATE KEY-----\n<a really big private Key>\n-----END PRIVATE KEY-----\n",
"client_email": "<clientEmail>",
"client_id": "<clientId>",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/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-hhay...."
}
我加载 JSON 文件的方式也与 firebase 网页相同:
对于行家:
使用 java 加载我的 json 文件:
FileInputStream serviceAccount = new FileInputStream("path/to/serviceAccountKey.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
.setDatabaseUrl("https://apilogintest-9c5f5.firebaseio.com/")
.build();
FirebaseApp initializeApp = FirebaseApp.initializeApp(options);
当我尝试验证令牌 ID 时的最后一部分:
FirebaseAuth.getInstance(initializeApp).verifyIdToken(token)
.addOnSuccessListener(new OnSuccessListener<FirebaseToken>() {
@Override
public void onSuccess(FirebaseToken decodedToken) {
String uid = decodedToken.getUid();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
e.printStackTrace();
}
});
我认为我做了与 firebase 网页提到的相同的操作,但我收到了上述错误
有人可以帮助我吗?
我会很感激你的帮助。