如何使用access_token和graph api检查用户是否登录到facebook

时间:2011-03-31 12:28:53

标签: facebook facebook-graph-api

我有一个facebook iframe应用程序。 我正在使用图形api向服务器端的facebook请求。 我有离线访问权限,因此access_token永不过期。

我想知道当前用户是否登录到facebook,所以当他退出facebook时我可以将他从我的应用程序中注销。 我不想使用javascript sdk。

我尝试过使用user表的online_presence字段但总是返回null。 我有什么方法可以做到这一点吗?

有更好的方法吗?

3 个答案:

答案 0 :(得分:3)

您需要另一个权限user_online_presence。 取自Facebook开发页面中的权限列表: http://developers.facebook.com/docs/authentication/permissions/

答案 1 :(得分:0)

我的申请需要同样的东西。据我所知,没有官方方法可以检查用户是否已登录,例如checkToken()方法。我所做的就是快速了解他的个人资料,看看我是否得到了答复。据我所知,如果用户退出Facebook,甚至你的offline_access令牌也将无效。

答案 2 :(得分:0)

回复较晚,但现在4.25.0的版本 Facebook SDK 有一种方法:

public void retrieveLoginStatus(Context context,
                                LoginStatusCallback responseCallback)

哪个州:

  

检索用户的登录状态。这将返回访问权限   用户登录Facebook for Android时的应用令牌   同一设备上的应用程序和该用户以前登录过的   应用程序。如果检索到访问令牌,则会显示Toast   告诉用户他们已经登录。

可以像:

一样使用
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();
    }
} );

已经回答here