[英]Sign in With Apple - Android Implementation with Firebase
setting up the Android Firebase on Android.在 Android 上设置 Android Firebase。
I need to get the unique identifier of the user, so that I can have consistency accross all platforms no matter if the email is hided or not.我需要获取用户的唯一标识符,这样无论 email 是否隐藏,我都可以在所有平台上保持一致性。
Based upon https://firebase.google.com/docs/auth/android/apple , using.startActivityForSignInWithProvider(this, provider.build()) we can see:基于https://firebase.google.com/docs/auth/android/apple , using.startActivityForSignInWithProvider(this, provider.build()) 我们可以看到:
public void onSuccess(AuthResult authResult) {
Log.d(TAG, "checkPending:onSuccess:" + authResult);
// Get the user profile with authResult.getUser() and
// authResult.getAdditionalUserInfo(), and the ID
// token from Apple with authResult.getCredential().
And from Apple https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_rest_api/authenticating_users_with_sign_in_with_apple#3383773 we can also see从Apple https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_rest_api/authenticating_users_with_sign_in_with_apple#3383773我们也可以看到
The identity token is a JSON Web Token (JWT) and contains
and和
sub
The subject registered claim identifies the principal that’s the subject of the identity token. Because this token is for your app, the value is the unique identifier for the user.
Question:问题:
What I got from firebase is AuthCredential, but I am expecting JWT with "sub" in it.我从 firebase 得到的是 AuthCredential,但我期待 JWT 里面有“sub”。
How can I get it?我怎么才能得到它?
thanks to Ricardo from Firebase support I got this working.感谢来自 Firebase 的 Ricardo 支持,我得到了这个工作。
in Android the sub value from Apple is in在 Android 中,来自 Apple 的子值位于
"firebase":{"identities":{"apple.com":["001814.24f212edfsdfdfsfd69b7b5fee972e.1722"],"email":["a@b.com"]},"sign_in_provider":"apple.com"}
You get it using你得到它使用
auth.startActivityForSignInWithProvider(this, provider.build()).addOnSuccessListener( new OnSuccessListener<AuthResult>() {
@Override
public void onSuccess(AuthResult authResult) {
// Sign-in successful!
FirebaseUser user = authResult.getUser();
AuthCredential identityToken = authResult.getCredential();
authResult.getUser().getIdToken(false).addOnSuccessListener(new OnSuccessListener<GetTokenResult>() {
@Override
public void onSuccess(GetTokenResult result) {
String idToken = result.getToken();
//Do whatever
Log.d(TAG, "GetTokenResult result = " + idToken);
try {
decodeJWT(idToken);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
})
Where the JWT is decoded with其中 JWT 被解码为
private static void decodeJWT(String JWTEncoded) throws Exception {
try {
String[] split = JWTEncoded.split("\\.");
Log.d("JWT_DECODED", "Header: " + getJson(split[0]));
Log.d("JWT_DECODED", "Body: " + getJson(split[1]));
} catch (UnsupportedEncodingException e) {
//Error
}
}
private static String getJson(String strEncoded) throws UnsupportedEncodingException{
byte[] decodedBytes = Base64.decode(strEncoded, Base64.URL_SAFE);
return new String(decodedBytes, "UTF-8");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.