简体   繁体   中英

How do I get the user's name, email, and profile picture when the user logs in with their Facebook account?

I have been able to create a successful login with the user's Facebook account on my android app. Now, I want to be able to get the user's name, email, and profile picture.

Similar to what I did when the user logs in with their Google account:

GoogleSignInAccount account = result.getSignInAccount();
String name = account.getDisplayName();
String email = account.getEmail();
String img_url = account.getPhotoUrl().toString();
Name.setText(name);
Username.setText(email);
Glide.with(this).load(img_url).into(Prof_Pic);

The above code shows what I did when the user logged in with their Google account. I want to pretty much do the same thing when the user logs in with their Facebook account. What do I do?

Use this after you get the user facebook accessToken :

public static void getFacebookUserInfo(AccessToken accessToken,
                                       GraphRequest.GraphJSONObjectCallback callback) {
    if (accessToken == null) {
        throw new NullPointerException("AccessToken should not be null !");
    }

    Bundle params = new Bundle();
    params.putString("fields", "name, email");
    GraphRequest request = GraphRequest.newMeRequest(accessToken, loginCallback);
    request.setParameters(params);
    request.executeAsync();
} 

and the callback :

private class FacebookUserInfoCallback implements GraphRequest.GraphJSONObjectCallback {
    @Override
    public void onCompleted(JSONObject object, GraphResponse response) {
        if (response.getError() == null) {
            String name = object.optString("name");
            String email = object.optString("email");
        }
    }
} 

and get the profile picture(should be call after facebook user logged in) :

Profile profile = Profile.getCurrentProfile();
if(profile != null) {
    Uri profilePictureUri = profile.getProfilePictureUri(width, height);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM