开发者

How to check if a user is logged into facebook using his access_token and graph api

I have a facebook iframe application. I am doing开发者_Python百科 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?


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜