我希望用户能够使用Facebook SDK
注册我的Android应用。要获取他们的信息,我正在使用权限" GraphRequest
"执行email
。和" public_profile
。"根据{{3}},用户的名字和姓氏应该可以在" first_name
"和" last_name
"领域。但是,当我单击该按钮时,我收到一条错误消息,指出first_name没有值。返回的JSON
数组沿着以下行:
{"name":"First M. Last","id":"1234567890123456"}
我尚未提交我的应用以供审核,因为它尚未完成。这可能与这个问题有关吗?
答案 0 :(得分:3)
根据官方文档,您访问public_profile权限的登录信息应如下所示:
LoginManager.getInstance().logInWithReadPermissions(Login.this, Arrays.asList("public_profile", "email"));
获取个人资料信息:
private void getFbDetails(final AccessToken accessToken) {
GraphRequest request = GraphRequest.newMeRequest(accessToken,
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Toast.makeText(Login.this, object.toString(), Toast.LENGTH_LONG).show();
Log.v("FB Details", object.toString());
if (object != null) {
name_fb = object.optString("name");
email_fb = object.optString("email");
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,first_name, last_name, email,link");
request.setParameters(parameters);
request.executeAsync();
}
答案 1 :(得分:1)
您可以将名字和姓氏作为单独的参数。你可以连接名字&在api传递的姓氏。我正在使用此方法在使用Facebook登录时获取用户详细信息。
// initalize the login button
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions("public_profile");
loginButton.setReadPermissions("email");
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
raphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
// Get facebook data from login
try {
Bundle bFacebookData = getFacebookData(object);
usernamestr = fbEmail;
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id, first_name, last_name, email,gender, birthday, location"); // Parámetros que pedimos a facebook
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
Toast.makeText(Login.this, "Login attempt canceled", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(FacebookException e) {
Toast.makeText(Login.this, "Login attempt failed", Toast.LENGTH_SHORT).show();
}
});
private Bundle getFacebookData(JSONObject object) throws JSONException {
Bundle bundle = new Bundle();
String id = object.getString("id");
try {
URL profile_pic = new URL("https://graph.facebook.com/" + id + "/picture?width=200&height=150");
bundle.putString("profile_pic", profile_pic.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
}
bundle.putString("idFacebook", id);
fbEmail = object.getString("email");
fbId = object.getString("id");
if (object.has("first_name"))
bundle.putString("first_name", object.getString("first_name"));
if (object.has("last_name"))
bundle.putString("last_name", object.getString("last_name"));
if (object.has("email"))
bundle.putString("email", object.getString("email"));
if (object.has("gender"))
bundle.putString("gender", object.getString("gender"));
if (object.has("birthday"))
bundle.putString("birthday", object.getString("birthday"));
if (object.has("location"))
bundle.putString("location", object.getJSONObject("location").getString("name"));
return bundle;
}