简体   繁体   English

使用 Apple 登录 - 使用 Firebase 实现 Android

[英]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.

相关问题 如何在 Flutter Android 上编写 Firebase 用 Apple 登录的功能 - How to write Firebase Functions for Sign in with Apple on Flutter Android Apple 登录出现错误“Invalid_client”firebase android - Apple Sign in getting error "Invalid_client" firebase android Firebase Flutter 登录苹果崩溃应用程序 - Firebase Flutter Sign In With Apple Crashes App Firebase 身份验证 - 使用 Apple 登录:nonce 返回 nil - Firebase Authentication - Sign In with Apple: nonce returns nil Xamarin Forms Firebase Auth OAuth sign in for Apple, Microsoft, Twitter, ...? - Xamarin Forms Firebase Auth OAuth sign in for Apple, Microsoft, Twitter, ...? iOS 应用程序在尝试使用 Firebase 身份验证“使用 Apple 登录”时崩溃 - iOS app crashes when trying to "Sign in with Apple" using Firebase Authentication 使用 Firebase 撤销 Android 中的 Apple 帐户访问权限 - Revoke Apple account access in Android using Firebase 苹果登录 android FireBase Auth 有问题 - Apple login in android with FireBase Auth has problem 用户没有退出 firebase android - User is not getting sign out firebase android 使用 Apple ID 登录 Firebase 时出现错误“invalid_request: Invalid web redirect url” - Getting error "invalid_request: Invalid web redirect url" when using Apple ID sign-in with Firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM