我已经能够在我的安卓应用程序上使用用户的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);
}