How to check if user is logged in with FB SDK 4.0 for Android?

前端 未结 6 1741
走了就别回头了
走了就别回头了 2020-11-30 18:56

A few days ago I implemented FB Login to my APP, and today I found out that most of the things I have implemented are now deprecated.

Before, I was using Sess

相关标签:
6条回答
  • 2020-11-30 18:59

    According to facebook documentation you can do this by:

    AccessToken accessToken = AccessToken.getCurrentAccessToken();
    boolean isLoggedIn = accessToken != null && !accessToken.isExpired();
    
    0 讨论(0)
  • 2020-11-30 19:00

    My dilemma of using AccessToken and AccessTokenTracker for checking login status is that when AccessToken is ready and callback function of the tracker called but profile may be not ready yet, thus I cannot get or display Facebooker's name at that moment.

    My solution is to check current profile != null and use its tracker for having Facebooker's name at the same time:

        ProfileTracker fbProfileTracker = new ProfileTracker() {
            @Override
            protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
                // User logged in or changed profile
            }
        };
    

    Check login status and then get user name:

    Profile profile = Profile.getCurrentProfile();
    if (profile != null) {
        Log.v(TAG, "Logged, user name=" + profile.getFirstName() + " " + profile.getLastName());
    }
    
    0 讨论(0)
  • 2020-11-30 19:11

    Its late to reply, but now in version 4.25.0 of Facebook SDK there is a method:

    public void retrieveLoginStatus(Context context,
                                    LoginStatusCallback responseCallback)
    

    Which states:

    Retrieves the login status for the user. This will return an access token for the app if a user is logged into the Facebook for Android app on the same device and that user had previously logged into the app. If an access token was retrieved then a toast will be shown telling the user that they have been logged in.

    And can be used like:

    LoginManager.getInstance().retrieveLoginStatus( this, new LoginStatusCallback()
    {
        @Override
        public void onCompleted( AccessToken accessToken )
        {
            GraphRequest request = GraphRequest.newMeRequest( accessToken, new GraphRequest.GraphJSONObjectCallback()
            {
                @Override
                public void onCompleted( JSONObject object, GraphResponse response )
                {
                    Log.e( TAG, object.toString() );
                    Log.e( TAG, response.toString() );
    
                    try
                    {
                        userId = object.getString( "id" );
                        profilePicture = new URL( "https://graph.facebook.com/" + userId + "/picture?width=500&height=500" );
                        Log.d( "PROFILE_URL", "url: " + profilePicture.toString() );
                        if ( object.has( "first_name" ) )
                        {
                            firstName = object.getString( "first_name" );
                        }
                        if ( object.has( "last_name" ) )
                        {
                            lastName = object.getString( "last_name" );
                        }
                        if ( object.has( "email" ) )
                        {
                            email = object.getString( "email" );
                        }
                        if ( object.has( "birthday" ) )
                        {
                            birthday = object.getString( "birthday" );
                        }
                        if ( object.has( "gender" ) )
                        {
                            gender = object.getString( "gender" );
                        }
    
                        Intent main = new Intent( LoginActivity.this, MainActivity.class );
                        main.putExtra( "name", firstName );
                        main.putExtra( "surname", lastName );
                        main.putExtra( "imageUrl", profilePicture.toString() );
                        startActivity( main );
                        finish();
                    }
                    catch ( JSONException e )
                    {
                        e.printStackTrace();
                    }
                    catch ( MalformedURLException e )
                    {
                        e.printStackTrace();
                    }
    
                }
            } );
            //Here we put the requested fields to be returned from the JSONObject
            Bundle parameters = new Bundle();
            parameters.putString( "fields", "id, first_name, last_name, email, birthday, gender" );
            request.setParameters( parameters );
            request.executeAsync();
        }
    
        @Override
        public void onFailure()
        {
            Toast.makeText( LoginActivity.this, "Could not log in.", Toast.LENGTH_SHORT ).show();
        }
    
        @Override
        public void onError( Exception exception )
        {
            Toast.makeText( LoginActivity.this, "Could not log in.", Toast.LENGTH_SHORT ).show();
        }
    } );
    
    0 讨论(0)
  • 2020-11-30 19:18

    I got it!

    First, make sure you have initialized your FB SDK. Second, add the following:

    accessTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken newAccessToken) {
                updateWithToken(newAccessToken);
            }
        };
    

    This will be called when there's a change on the Current Access Tokes. Meaning, this will only help you if the user is already logged in.

    Next, we add this to our onCreate() method:

    updateWithToken(AccessToken.getCurrentAccessToken());
    

    Then of course, our updateWithToken() method:

    private void updateWithToken(AccessToken currentAccessToken) {
    
        if (currentAccessToken != null) {
            new Handler().postDelayed(new Runnable() {
    
                @Override
                public void run() {
                    Intent i = new Intent(SplashScreen.this, GeekTrivia.class);
                    startActivity(i);
    
                    finish();
                }
            }, SPLASH_TIME_OUT);
        } else {
            new Handler().postDelayed(new Runnable() {
    
                @Override
                public void run() {
                    Intent i = new Intent(SplashScreen.this, Login.class);
                    startActivity(i);
    
                    finish();
                }
            }, SPLASH_TIME_OUT);
        }
    }
    

    That did it for me! =]

    0 讨论(0)
  • 2020-11-30 19:18

    You can use the same way Felipe mentioned in his answer or you can use the other two ways. But it seems AccessTokenTracker is the convenient way, since it helps you to track the access tokens (use with ProfileTracker class)

    1. If you are using a custom button for your login use LoginManager call back

    For example

    In your layout xml

        <Button
            android:id="@+id/my_facebook_button"
            android:background="@drawable/btnfacebook"
            android:onClick="facebookLogin"/>
    

    In your Activity

        //Custom Button
        Button myFacebookButton = (Button) findViewById(R.id.my_facebook_button);
    

    The button onclick Listener

    public void facebookLogin(View view) {
            LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends"));
        }
    

    At the end the LoginManager Callback

     //Create callback manager to handle login response
            CallbackManager callbackManager = CallbackManager.Factory.create();
    
           LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
               @Override
               public void onSuccess(LoginResult loginResult) {
                   Log.i(TAG, "LoginManager FacebookCallback onSuccess");
                   if(loginResult.getAccessToken() != null) {
                       Log.i(TAG, "Access Token:: " + loginResult.getAccessToken());
                       facebookSuccess();
                   }
               }
    
               @Override
               public void onCancel() {
                   Log.i(TAG, "LoginManager FacebookCallback onCancel");
               }
    
               @Override
               public void onError(FacebookException e) {
                   Log.i(TAG, "LoginManager FacebookCallback onError");
               }
           });
    
    1. If you are using the button (com.facebook.login.widget.LoginButton) provided in SDK use LoginButton callback (This is crealy detailed in their reference doc - https://developers.facebook.com/docs/facebook-login/android/v2.3)

    For example

    In your layout xml

    <com.facebook.login.widget.LoginButton
                    android:id="@+id/login_button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"/>
    

    In your activity

        //Facebook SDK provided LoginButton
        LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.setReadPermissions("user_friends");
        //Callback registration
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                // App code
                Log.i(TAG, "LoginButton FacebookCallback onSuccess");
                if(loginResult.getAccessToken() != null){
                    Log.i(TAG, "Access Token:: "+loginResult.getAccessToken());
                    facebookSuccess();
                }
    
            }
    
            @Override
            public void onCancel() {
                // App code
                Log.i(TAG, "LoginButton FacebookCallback onCancel");
            }
    
            @Override
            public void onError(FacebookException exception) {
                // App code
                Log.i(TAG, "LoginButton FacebookCallback onError:: "+exception.getMessage());
                Log.i(TAG,"Exception:: "+exception.getStackTrace());
            }
        });
    

    Dont forget to call callbackManager.onActivityResult(requestCode, resultCode, data); in your Activity onActivityResult()

    0 讨论(0)
  • 2020-11-30 19:23

    A much simpler solution worked for my case (I don't know if this is the more elegant way though):

    public boolean isLoggedIn() {
        AccessToken accessToken = AccessToken.getCurrentAccessToken();
        return accessToken != null;
    }
    
    0 讨论(0)
提交回复
热议问题