2

I did the base64 utf-8 encoding of the email. Because it is a database key. However, the following problems arise. I need help.

setValue at /user/aW1hZ2VfNTk1NkBuYXZlci5jb20= failed: DatabaseError: Invalid token in path

databaseReference.child("user").child(util.getBase64encode(email)).setValue(userModel)

help me...

KENdi
  • 7,576
  • 2
  • 16
  • 31
jin
  • 51
  • 3
  • log the base64 encrypted email and check if it's the same at your database – Oussema Aroua May 24 '17 at 13:52
  • Works for me: http://jsbin.com/nemakev/edit?js,console which writes https://stackoverflow.firebaseio.com/44160089.json – Frank van Puffelen May 24 '17 at 14:18
  • 1
    The slash character, /, is not allowed in a Firebase token. Many Base64 encodings use '/'. Are you using a Base64 encoder that does not produce /. For example, [uses this option](https://developer.android.com/reference/android/util/Base64.html#URL_SAFE). – Bob Snyder May 24 '17 at 14:39

1 Answers1

0

In my case, the encoded string was having a new line at the end. Using Base64.NO_WRAP as mentioned by @Arvin in this answer worked as a solution. My encoding function now looks like:

public static String encodeToBase64(String strToEncode) {
    byte[] data = null;
    try {
        data = strToEncode.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return Base64.encodeToString(data, Base64.NO_WRAP);
}

If you're aiming to be safe like what @BobSnyder mentioned in the comments above, you could still use URL_SAFE and just trim the String just to make sure.

AL.
  • 36,815
  • 10
  • 142
  • 281