I have never done this in Java, so I donβt have a complete answer, but the fact that you sometimes lose one or sometimes two characters from the end of the line suggests that this may be a problem with Base64 add-on. You might want to infer the payload value and see if "=" ever ends, then jsonString will not be "}", and when the payload ends with "==", then jsonString will not be ""}. If it looks like something is going wrong with the interpretation of equal signs at the end of the payload, which should represent empty bits.
Edit: For further thought, I believe this is because Facebook uses Base64 URL encoding (which does not add = like pad characters) instead of regular Base64, while your decoding function expects regular Base64 with trailing = chars.
source share