繁体   English   中英

当用户使用其Facebook帐户登录时,如何获取用户的姓名,电子邮件和个人资料图片?

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

我已经能够在我的android应用程序上使用用户的Facebook帐户创建成功的登录名。 现在,我希望能够获得用户的姓名,电子邮件和个人资料图片。

与用户使用其Google帐户登录时的操作类似:

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);

上面的代码显示了用户使用其Google帐户登录时的操作。 当用户使用其Facebook帐户登录时,我几乎想做同样的事情。 我该怎么办?

在获得用户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();
} 

和回调:

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");
        }
    }
} 

并获取个人资料图片(Facebook用户登录后应调用):

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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