Get Facebook User ID from Android SDK 4.0

How can I get Facebook User ID from Android Android SDK 4.0?

Here is my code that doesn't work:

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    setContentView(R.layout.activity_firstlogin);

    CallbackManager cm;
    LoginButton     lb;

    cm = CallbackManager.Factory.create();
    lb = (LoginButton) findViewById(R.id.facebook_button);
    Log.d(TAG, "Salut les zouzous");
    LoginManager.getInstance().registerCallback(cm,
            new FacebookCallback<LoginResult>()
            {
                @Override
                public void onSuccess(LoginResult loginResult)
                {
                   Log.d(TAG, "Success !");
                    AccessToken tok;
                    tok = AccessToken.getCurrentAccessToken();
                    Log.d(TAG, tok.getUserId());
                }

                @Override
                public void onCancel()
                {
                    Log.d(TAG, "On Cancel");
                }

                @Override
                public void onError(FacebookException exception)
                {
                    Log.e(TAG, exception.getMessage());
                }
            });

      

I know for sure that

 Log.d(TAG, " Success"); 

      

does not print.

There is only the first log that prints something, outside of the callback.

Sorry for the bad english.

+3


source to share


4 answers


Easier than

Profile.getCurrentProfile().getId()

      



It is assumed that the user is already signed in.

+22


source


You will need to do the following



 @Override
        public void onSuccess(LoginResult loginResult) {
            final AccessToken accessToken = loginResult.getAccessToken();

            GraphRequestAsyncTask request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject user, GraphResponse graphResponse) {
                    Log.d(TAG, user.optString("email"));
                    Log.d(TAG, user.optString("name"));
                    Log.d(TAG, user.optString("id"));
                }
            }).executeAsync();
        }

      

+8


source


just use loginResult.getAccessToken().getUserId()

in onSuccess () like

@Override
public void onSuccess(LoginResult loginResult) {

    Toast.makeText(Login.this, "User ID : "+loginResult.getAccessToken().getUserId(), Toast.LENGTH_LONG).show();

}

      

+4


source


If you are using the login button that is provided by Facebook I think instead of

LoginManager.getInstance().registerCallback()

you need to use

lb.registerCallback()

This might explain why your callback isn't firing at all.

**** **** edit

If your callback still fails and you only need the user id, you can go instead of tracking the AccessToken.

Facebook offers an example where you override the onActivityResult method for an Activity / Fragment that you declared in a button:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}

      

0


source







All Articles