How to get FB User ID using Facebook Login Button in android application

前端 未结 8 2511
鱼传尺愫
鱼传尺愫 2021-02-05 21:51

I\'m developing an application in which I\'m using Facebook log in button from SDK. I\'m able to get access_token of the user but I want userID

相关标签:
8条回答
  • 2021-02-05 22:26

    You should be used new facebook sdk 3.0 and

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
            if (Session.getActiveSession().isOpened()) {
                // Request user data and show the results
                Request.executeMeRequestAsync(Session.getActiveSession(), new Request.GraphUserCallback() {
    
                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        // TODO Auto-generated method stub
                        if (user != null) {
                            // Display the parsed user info
                            Log.v(TAG, "Response : " + response);
                            Log.v(TAG, "UserID : " + user.getId());
                            Log.v(TAG, "User FirstName : " + user.getFirstName());
    
                        }
                    }
    
                });
            }
        }
    
    0 讨论(0)
  • 2021-02-05 22:30

    Use the getId method of FB Profile class( Facebook SDK 4.0).

      Profile profile = Profile.getCurrentProfile();
      System.out.println(profile.getFirstName());
      System.out.println(profile.getId());
    
    0 讨论(0)
  • 2021-02-05 22:32

    After successful login, you need to call following code. You can get the logged-in user details with id in response.

    Session session = Session.getActiveSession();
    Request request = Request.newGraphPathRequest(session, "me", null);
    com.facebook.Response response = Request.executeAndWait(request)
    
    0 讨论(0)
  • 2021-02-05 22:32

    The Facebook graph API lets you make calls that return JSON, which you can parse in Android with a JSONObject like this below code.

    JSONObject me = new JSONObject(fb.request("me"));
    String id = me.getString("id");
    
    0 讨论(0)
  • 2021-02-05 22:42

    As these all answers are for older SDK , for SDK 4.0 here is the code

    loginButton.registerCallback(callbackManager, new    FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                // App code
                Profile profile = Profile.getCurrentProfile();
    
                Log.d("facebook",profile.getId());
            }
    
            @Override
            public void onCancel() {
                // App code
                Log.d("facebook","failed");
            }
    
            @Override
            public void onError(FacebookException exception) {
                // App code
                Log.d("facebook","failed");
            }
        });
    

    i was searching for the same thing looks like the Session object is no more accessible so above is the new way to get user profile and user id.

    0 讨论(0)
  • 2021-02-05 22:42

    You can get the user ID with this code :

    document.getElementById('status').innerHTML =
       'Welcome ' + response.name + '!' + "<br />"+
       'Your user ID is : ' + response.id;
    
    0 讨论(0)
提交回复
热议问题