How to get user profile information from Facebook API Android

前端 未结 6 2052
半阙折子戏
半阙折子戏 2020-12-05 08:53

I\'m trying to get user profile information Facebook. Begins loading, I click to confirm permission to send my profile, again, continue loading but eventually get empty fiel

相关标签:
6条回答
  • 2020-12-05 09:14

    This code for facebook SDK 3.0 It`s works!

    public class FacebookInfo extends Activity {
    ...
    String get_id, get_name, get_gender, get_email, get_birthday, get_locale, get_location;
    
    private Session.StatusCallback fbStatusCallback = new Session.StatusCallback() {
        public void call(Session session, SessionState state, Exception exception) {
            if (state.isOpened()) {
                Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
                    public void onCompleted(GraphUser user, Response response) {
                        if (response != null) {
                            // do something with <response> now
                            try{
                                get_id = user.getId();
                                get_name = user.getName();
                                get_gender = (String) user.getProperty("gender");
                                get_email = (String) user.getProperty("email");
                                get_birthday = user.getBirthday();
                                get_locale = (String) user.getProperty("locale");
                                get_location = user.getLocation().toString();   
    
                            Log.d(LOG_TAG, user.getId() + "; " +  
                                user.getName() + "; " +
                                (String) user.getProperty("gender") + "; " +        
                                (String) user.getProperty("email") + "; " +
                                user.getBirthday()+ "; " +
                                (String) user.getProperty("locale") + "; " +
                                user.getLocation());
                            } catch(Exception e) {
                                 e.printStackTrace();
                                 Log.d(LOG_TAG, "Exception e");
                             }
    
                        }
                    }
                });
            }
        }
    };
    ...
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fbinfo);
        try {
            openActiveSession(this, true, fbStatusCallback, Arrays.asList(
                    new String[] { "email",  "user_location", "user_birthday",
     "user_likes", "publish_actions" }), savedInstanceState);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
      private  Session openActiveSession(Activity activity, boolean allowLoginUI,
     StatusCallback callback, List<String> permissions, Bundle savedInstanceState) {
        OpenRequest openRequest = new OpenRequest(activity).
    setPermissions(permissions).setLoginBehavior(SessionLoginBehavior.
    SSO_WITH_FALLBACK).setCallback(callback).
    setDefaultAudience(SessionDefaultAudience.FRIENDS);
    
        Session session = Session.getActiveSession();
        Log.d(LOG_TAG, "" + session);
        if (session == null) {
            Log.d(LOG_TAG, "" + savedInstanceState);
            if (savedInstanceState != null) {
                session = Session.restoreSession(this, null, fbStatusCallback, savedInstanceState);
            }
            if (session == null) {
                session = new Session(this);
            }
            Session.setActiveSession(session);
            if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED) || allowLoginUI) {
                session.openForRead(openRequest);
                return session;
            }
        }
        return null;
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
    }
    }
    
    0 讨论(0)
  • 2020-12-05 09:15

    For those who's still struggling in new Graph API 3.0 here's the solution works for Graph API 2.12 or below.

      //Request a read permission of user's info from Facebook
        //Data provided by Facebook will be used for Firebase FireStore
        LoginManager.getInstance().logInWithReadPermissions(LogIn.this, Arrays.asList("email", "public_profile"));
    
        LoginManager.getInstance().registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(final LoginResult loginResult) {
    
                mStateOfSuccess = false;
                //Dismiss any snackbar first before showing a new one
                mSnackBar.dismiss();
                mSnackBar.show();
    
                Log.d(TAG, "facebook:onSuccess:" + loginResult);
    
                //Bundle is use for passing data as K/V pair like a Map
                Bundle bundle=new Bundle();
                //Fields is the key of bundle with values that matched the proper Permissions Reference provided by Facebook
                bundle.putString("fields","id, email, first_name, last_name, gender,age_range");
    
                //Graph API to access the data of user's facebook account
                GraphRequest request = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject object, GraphResponse response) {
                                Log.v("Login Success", response.toString());
    
                                //For safety measure enclose the request with try and catch
                                try {
    
                                    //The get() or getString() key should be included in Bundle otherwise it won't work properly
                                    //If not then error dialog will be called
    
                                    //First re-initialize jSON object to a new Contructor with parameter that is equal to a jSON format age range
                                    JSONObject ageRange  = new JSONObject(object.getString("age_range"));
    
                                            //Log in using Facebook with Firebase
                                            loginToFirebaseUsingFacebook(loginResult.getAccessToken()
                                                    ,object.getString("first_name")
                                                    ,object.getString("last_name")
                                                    //Then get again get a string from object itself for the minimum age range
                                                    //The idea is that we need to get minimum age only written in string format
                                                    //not the whole age range data that is written in jSON format
                                                    ,ageRange.getString("min")
                                                    ,object.getString("gender")
                                                    ,object.getString("email")
                                            );
    
                                }
                                //If no data has been retrieve throw some error
                                catch (JSONException e) {
                                   ErrorDialog(e.getMessage(),"facebookAuth");
                                }
    
                            }
                        });
    
    
                //Set the bundle's data as Graph's object data
                request.setParameters(bundle);
    
                //Execute this Graph request asynchronously
                request.executeAsync();
    
            }
    
            @Override
            public void onCancel() {
                Log.d(TAG, "facebook:onCancel");
                ErrorDialog("Request has canceled.","facebookAuth");
            }
    
            @Override
            public void onError(FacebookException error) {
                Log.d(TAG, "facebook:onError", error);
                ErrorDialog(String.valueOf(error),"facebookAuth");
            }
        });
    
    
    }
    

    Note that using age range requires App Review if will use in the production, meaning you may just try to use this on your own account (admin or developers account). Visit: https://developers.facebook.com/docs/facebook-login/permissions/?locale=en_US#reference-public_profile for more details.

    0 讨论(0)
  • 2020-12-05 09:21
    //register callback object for facebook result
        LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
    
                //By Profile Class
                Profile profile = Profile.getCurrentProfile();
                if (profile != null) {
                    facebook_id=profile.getId();
                    f_name=profile.getFirstName();
                    m_name=profile.getMiddleName();
                    l_name=profile.getLastName();
                    full_name=profile.getName();
                    profile_image=profile.getProfilePictureUri(400, 400).toString();
                }
                //Toast.makeText(FacebookLogin.this,"Wait...",Toast.LENGTH_SHORT).show();
                GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject object, GraphResponse response) {
                                try {
                                    email_id=object.getString("email");
                                    gender=object.getString("gender");
                                    String profile_name=object.getString("name");
                                    long fb_id=object.getLong("id"); //use this for logout
    
                                } catch (JSONException e) {
                                    // TODO Auto-generated catch block
                                    //  e.printStackTrace();
                                }
    
                            }
    
                        });
    
                request.executeAsync();
            }
    
            @Override
            public void onCancel() {
                Toast.makeText(FacebookLogin.this,getResources().getString(R.string.login_canceled_facebooklogin),Toast.LENGTH_SHORT).show();
                progress.dismiss();
            }
    
            @Override
            public void onError(FacebookException error) {
                Toast.makeText(FacebookLogin.this,getResources().getString(R.string.login_failed_facebooklogin),Toast.LENGTH_SHORT).show();
                progress.dismiss();
            }
        });
    
    0 讨论(0)
  • 2020-12-05 09:33

    Request.executeMeRequestAsync is deprecated! So, do like this:

    Once your session ready:

    private void getUserData(Session session, SessionState state)
    {
        if (state.isOpened())
        {
            Request.newMeRequest(session, new Request.GraphUserCallback()
            {
                @Override
                public void onCompleted(GraphUser user, Response response)
                {
                    if (response != null)
                    {
                        try
                        {
                            String name = user.getName();
                            // If you asked for email permission
                            String email = (String) user.getProperty("email");
                            Log.e(LOG_TAG, "Name: " + name + " Email: " + email);
                        }
                        catch (Exception e)
                        {
                            e.printStackTrace();
                            Log.d(LOG_TAG, "Exception e");
                        }
    
                    }
                }
            }).executeAsync();
        }
    }
    
    0 讨论(0)
  • 2020-12-05 09:34

    first create object and veriable for facebook:

    private static String FACEBOOK_APP_ID = "492429660800628";
    private Facebook facebook;
    private AsyncFacebookRunner mAsyncRunner;
    

    after OnCreate Method :

    protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_screen);
    
        facebook = new Facebook(FACEBOOK_APP_ID);
        mAsyncRunner = new AsyncFacebookRunner(facebook);
    
        loginFacebook();//this method when called when you required..
    
    }
    
    
    
    private void loginFacebook() {
    
        if (!facebook.isSessionValid()) {
    
            facebook.authorize(this, new String[] { "email", "publish_stream",
                    "read_stream" }, new LoginDialogListener());
    
        } else {
    
            getProfileInformation();
    
        }
    
    }
    
    
    
    class LoginDialogListener implements DialogListener {
    
        public void onComplete(Bundle values) {
            try {
    
                getProfileInformation();
    
            } catch (Exception error) {
                Toast.makeText(LoginActivity.this, error.toString(),
                        Toast.LENGTH_SHORT).show();
            }
        }
    
        public void onFacebookError(FacebookError error) {
            Toast.makeText(LoginActivity.this,
                    "Something went wrong. Please try again.",
                    Toast.LENGTH_LONG).show();
        }
    
        public void onError(DialogError error) {
            Toast.makeText(LoginActivity.this,
                    "Something went wrong. Please try again.",
                    Toast.LENGTH_LONG).show();
        }
    
        public void onCancel() {
            Toast.makeText(LoginActivity.this,
                    "Something went wrong. Please try again.",
                    Toast.LENGTH_LONG).show();
        }
    }
    

    please try this method after login facebook:

    public void getProfileInformation() {
    
    
        try {
    
            JSONObject profile = Util.parseJson(facebook.request("me"));
            Log.e("Profile", "" + profile);
    
            mUserId = profile.getString("id");
            mUserToken = facebook.getAccessToken();
            mUserName = profile.getString("name");
            mUserEmail = profile.getString("email");
    
            runOnUiThread(new Runnable() {
    
                public void run() {
    
                    Log.e("FaceBook_Profile",""+mUserId+"\n"+mUserToken+"\n"+mUserName+"\n"+mUserEmail);
    
                    Toast.makeText(getApplicationContext(),
                            "Name: " + mUserName + "\nEmail: " + mUserEmail,
                            Toast.LENGTH_LONG).show();
    
    
    
                }
    
            });
    
        } catch (FacebookError e) {
    
            e.printStackTrace();
        } catch (MalformedURLException e) {
    
            e.printStackTrace();
        } catch (JSONException e) {
    
            e.printStackTrace();
        } catch (IOException e) {
    
            e.printStackTrace();
        }
    
    }
    
    0 讨论(0)
  • 2020-12-05 09:35

    First: login

    LoginManager loginManager = LoginManager.getInstance();
    loginManager.registerCallback(callbackManager, new FacebookLoginCallback());
    

    Then: get profile infomation

    // call this when login success
    Profile profile = Profile.getCurrentProfile();
    // getAvatar
    String avatar = ImageRequest.getProfilePictureUri(profile.getId(), width, height).toString();
    
    0 讨论(0)
提交回复
热议问题