繁体   English   中英

如果我使用 facebook 进行身份验证,如何获取 firebase auth uid?

[英]How get firebase auth uid if I'm using facebook for authentication?

Facebook 认证成功但我想知道如何获得 firebase auth.uid 这是我的 android 应用程序防火墙 facebook 登录代码。

登录.活动:

  FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_login);
    
        loginButton = (LoginButton) findViewById(R.id.login_button);
        ImageView ivLoginScreenImage = (ImageView) findViewById(R.id.ivLoginScreenImage);
        Glide.with(this)
                .load(R.drawable.image_swing)
                .fitCenter()
                .into(ivLoginScreenImage);
        callbackManager = CallbackManager.Factory.create();
    
      loginButton.setReadPermissions(Arrays.asList("public_profile", "email"));
    
    
    
    
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(final LoginResult loginResult) {
                loginButton.setVisibility(View.INVISIBLE);
                Log.d(TAG, "Login Success " + loginResult.toString());
                handleFacebookAccessToken(loginResult.getAccessToken());
    
    
    
    
                ProfileTracker profileTracker = new ProfileTracker() {
                    @Override
                    protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
                        this.stopTracking();
                        Profile.setCurrentProfile(currentProfile);
                        Log.d(TAG,"Setting current profile " + Profile.getCurrentProfile().getFirstName());
                        GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
                                new GraphRequest.GraphJSONObjectCallback() {
                                    @Override
                                    public void onCompleted(JSONObject object, GraphResponse response) {
                                        User user = User.fromJSON(object);
                                        Log.d(TAG, "Returning logged user info " + user.getName() + " " + " " + user.getEmail());
                                        //On Successful login
                                        Intent returnIntent = new Intent();
                                        returnIntent.putExtra("loggedUser", user);
                                        setResult(Activity.RESULT_OK, returnIntent);
                                        finish();
                                    }
                                });
                        Bundle parameters = new Bundle();
                        parameters.putString("fields", "id, name, email, gender, age_range, link");
                        request.setParameters(parameters);
                        request.executeAsync();
                    }
                };
                profileTracker.startTracking();
            }
    
    
            @Override
            public void onCancel() {
                Log.d(TAG, "Cancelled");
                Intent returnIntent = new Intent();
                setResult(Activity.RESULT_CANCELED, returnIntent);
                finish();
            }
    
            @Override
            public void onError(FacebookException e) {
                Log.e(TAG, "Error " + e.toString());
                Intent returnIntent = new Intent();
                setResult(Activity.RESULT_CANCELED, returnIntent);
                finish();
            }
    
    
        });
    }
    private void handleFacebookAccessToken(AccessToken token) {
        Log.d(TAG, "handleFacebookAccessToken:" + token);
        mAuth = FirebaseAuth.getInstance();
    
    
        AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            Log.d(TAG, "signInWithCredential:success");
                            FirebaseUser user = mAuth.getCurrentUser();
    
    
                            if (user != null) {
                                Log.i(TAG, "email" + user.getEmail());
                            }
    
                        } else {
                            // If sign in fails, display a message to the user.
                            Log.w(TAG, "signInWithCredential:failure", task.getException());
    
                        }
    
    
                    }
                });
    }
    
    
    
    
    
    
    
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);

}

我已经尝试过这段代码,并在将更新到 Firestore 的代码中调用了userr 登录成功,但这个用户是 null 所以我得到以下错误

FirebaseAuth mAuth;
mAuth =FirebaseAuth.getInstance();
     FirebaseUser userr = mAuth.getCurrentUser().getUid;

Error as Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference

请各位大侠帮忙。

handleFacebookAccessToken()

if (task.isSuccessful()) {
                       
        Log.d(TAG, "signInWithCredential:success");
        FirebaseUser user = mAuth.getCurrentUser();

                if (user != null) {
                    Log.i(TAG, "email" + user.getEmail());
                    //--------------Here is the UID
                    Log.i(TAG, "uid" + user.getUid());
                }

         }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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