[英]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. 我已经能够在我的android应用程序上使用用户的Facebook帐户创建成功的登录名。 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: 与用户使用其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);
The above code shows what I did when the user logged in with their Google account. 上面的代码显示了用户使用其Google帐户登录时的操作。 I want to pretty much do the same thing when the user logs in with their Facebook account.
当用户使用其Facebook帐户登录时,我几乎想做同样的事情。 What do I do?
我该怎么办?
Use this after you get the user facebook accessToken : 在获得用户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) : 并获取个人资料图片(Facebook用户登录后应调用):
Profile profile = Profile.getCurrentProfile();
if(profile != null) {
Uri profilePictureUri = profile.getProfilePictureUri(width, height);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.