我是Facebook SDK的新手。 有没有人知道如何在Android平台上从Facebook SDK获取名字,姓氏,电子邮件,ID,生日,性别,家乡,年龄等?
请告诉我代码?
下面是我尝试过的
callbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions( "public_profile", "email", "user_birthday", "user_friends");
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = AccessToken.getCurrentAccessToken();
boolean isLoggedIn = accessToken != null && !accessToken.isExpired();
GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
// Insert your code here
try {
if (object != null) {
JSONObject obj = response.getJSONObject();
id = obj.getString("id");
name = obj.getString("name");
birthday = obj.getString("birthday");
email = obj.getString("email");
String gender = obj.getString("gender");
showUserInfoToast();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,email,first_name,last_name,gender");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
});
我尝试过,但是不起作用。 请帮助
答案 0 :(得分:0)
您访问的数据取决于某人授予您的应用程序的权限以及他们选择与应用程序共享的数据(Link)
这些字段可以是null
答案 1 :(得分:0)
请尝试使用此代码。希望它将获取您需要的所有数据。谢谢
public void onSuccess(final LoginResult loginResult) {
Log.e("bug", "facebook sign in success");
if (loginResult != null) {
Profile profile = Profile.getCurrentProfile();
if (profile != null) {
firstName = profile.getFirstName();
lastName = profile.getLastName();
social_id = profile.getId();
profileURL = String.valueOf(profile.getProfilePictureUri(200, 200));
Log.e(TAG, "social Id: " + profileURL);
}
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.e("bug", "facebook social_id=>" + social_id + " toeken" + loginResult.getAccessToken().getToken());
Log.e("bug", "graph response=>" + object.toString());
try {
if (object.has("name")) {
String[] name = object.getString("name").split(" ");
firstName = name[0];
lastName = name[1];
}
email = object.has("email") ? object.getString("email") : "";
social_id = object.has("id") ? object.getString("id") : "";
socialSignUp("facebook");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,first_name,name,last_name,email,gender,birthday");
request.setParameters(parameters);
request.executeAsync();
}
答案 2 :(得分:0)