I'm trying to use Firebase email and password authentication in Java using their REST API, as their Admin SDK doesn't provide the needed methods to log in etc., only user management methods.
With help from this answer, I've managed to put together the following code, which works for correct credentials but when trying to handle errors e.g. USER_NOT_FOUND or INVALID_PASSWORD, all I get is a java.io.IOException with the details Server returned HTTP response code: 400 for URL: https://www.googleapis.com/identitytoolkit/v3/relyingparty/getAccountInfo?key=key.
package com.amansprojects.craftclaw;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class FirebaseAuthManager {
private static final String BASE_URL = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/";
private static final String OPERATION_AUTH = "verifyPassword";
private static final String OPERATION_ACCOUNT_INFO = "getAccountInfo";
private String firebaseKey;
private static FirebaseAuthManager instance = null;
protected FirebaseAuthManager() {
firebaseKey = "MY_KEY_HERE";
}
public static FirebaseAuthManager getInstance() {
if (instance == null) {
instance = new FirebaseAuthManager();
}
return instance;
}
public String auth(String username, String password) {
HttpURLConnection urlRequest = null;
String token = null;
try {
URL url = new URL(BASE_URL + OPERATION_AUTH + "?key=" + firebaseKey);
urlRequest = (HttpURLConnection) url.openConnection();
urlRequest.setDoOutput(true);
urlRequest.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
OutputStream os = urlRequest.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
osw.write("{\"email\": \"" + username + "\", \"password\": \"" + password + "\", \"returnSecureToken\": true}");
osw.flush();
osw.close();
urlRequest.connect();
JsonParser jp = new JsonParser();
JsonElement root = jp.parse(new InputStreamReader((InputStream) urlRequest.getContent()));
JsonObject rootObj = root.getAsJsonObject();
token = rootObj.get("idToken").getAsString();
System.out.println(rootObj); // debugging
} catch (IOException e) { e.printStackTrace(); return null; }
finally { urlRequest.disconnect(); }
return token;
}
public String getAccountInfo(String token) {
HttpURLConnection urlRequest = null;
String email = null;
try {
URL url = new URL(BASE_URL + OPERATION_ACCOUNT_INFO + "?key=" + firebaseKey);
urlRequest = (HttpURLConnection) url.openConnection();
urlRequest.setDoOutput(true);
urlRequest.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
OutputStream os = urlRequest.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
osw.write("{\"idToken\": \"" + token + "\"}");
osw.flush();
osw.close();
urlRequest.connect();
JsonParser jp = new JsonParser();
JsonElement root = jp.parse(new InputStreamReader((InputStream) urlRequest.getContent()));
JsonObject rootObj = root.getAsJsonObject();
email = rootObj.get("users").getAsJsonArray().get(0).getAsJsonObject().get("email").getAsString();
} catch (IOException e) { e.printStackTrace(); return null; }
finally { urlRequest.disconnect(); }
return email;
}
}
Thanks in advance.
Edit: I’ve also tried with the other domain/endpoint combination that Firebase shows on their docs page: https://identitytoolkit.googleapis.com/v1/accounts:endpoint