繁体   English   中英

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

[英]Sign in With Apple - Android Implementation with Firebase

在 Android 上设置 Android Firebase。

我需要获取用户的唯一标识符,这样无论 email 是否隐藏,我都可以在所有平台上保持一致性。

基于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().

从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

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.

问题:

我从 firebase 得到的是 AuthCredential,但我期待 JWT 里面有“sub”。

我怎么才能得到它?

感谢来自 Firebase 的 Ricardo 支持,我得到了这个工作。

在 Android 中,来自 Apple 的子值位于

"firebase":{"identities":{"apple.com":["001814.24f212edfsdfdfsfd69b7b5fee972e.1722"],"email":["a@b.com"]},"sign_in_provider":"apple.com"}

你得到它使用

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();
                                    }


                                }
                            });



                        }
                    })

其中 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM