0

I have a facebook iframe application. I am doing my requests to facebook on server side using the graph api. I have offline access so the access_token never expires.

I would like to know if the current user is logged into facebook so when he logs out of facebook i can log him out of my application. I do not want to use the javascript sdk.

I have tried using the online_presence field of the user table but that always returns null. Is there any way i can do this?

Is there a better way about it?

Daniel
  • 2,331
  • 3
  • 26
  • 35
  • 1
    I think subscribing to the events in the js sdk is the only way, since logging out is a client side event. I don't think there is a server-side alternative. FB.Event.subscribe('auth.sessionChange', function(response) – Elad Lachmi Mar 31 '11 at 12:34
  • Thanks. - it seems to only work after a page refresh and recalling init. before hand the session is still there. – Daniel Mar 31 '11 at 15:04

3 Answers3

3

You need another permission, user_online_presence. Taken from the permissions list in the Facebook dev pages: http://developers.facebook.com/docs/authentication/permissions/

Elad Lachmi
  • 10,406
  • 13
  • 71
  • 133
0

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

This was already answered here.

Talha
  • 903
  • 8
  • 31
0

I needed the same thing for my application. As far as I saw, there's no official way to check if a user is logged in, such as a checkToken() method. What I do is a quick pull of his profile and see if I get a response. Even your offline_access token will become invalid if the user logs out of Facebook as far as I know.

Christof
  • 3,777
  • 4
  • 37
  • 49
  • Nope. Logging out will not invalidate the offline_access token. That's the point :) – Elad Lachmi Mar 31 '11 at 12:37
  • For me offline != logged out. I was assuming that offline_access was synonymous for "server_access", as in, I don't have to, e.g., post to a users wall with him like with the JS SDK. Thanks for clearing that up. – Christof Mar 31 '11 at 12:47
  • offline_access means you can authenticate with this token at any given time. The user needs to revoke the permission in order to invalidate the token. – Elad Lachmi Mar 31 '11 at 12:50
  • There IS a way, now in 4.25.0 atleast. :) – Talha Jul 31 '17 at 07:51