我已经能够在我的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帐户登录时,我几乎想做同样的事情。我该怎么办?
答案 0 :(得分:0)
在获得用户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);
}