我使用Facebook registerCallBack方法获取当前用户信息。我遇到的问题是在尝试检索电子邮件地址时。
// Callback registration
FBLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
final Profile profile = Profile.getCurrentProfile();
if (profile != null) {
}
//Toast.makeText(FacebookLogin.this,"Wait...",Toast.LENGTH_SHORT).show();
GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
String email_id = object.getString("email");
String gender = object.getString("gender");
String facebook_id=profile.getId();
String f_name=profile.getFirstName();
String m_name=profile.getMiddleName();
String l_name=profile.getLastName();
String full_name=profile.getName();
String profile_image=profile.getProfilePictureUri(400, 400).toString();
Log.i("facebook_id",facebook_id);
Log.i("f_name",f_name);
Log.i("m_name",m_name);
Log.i("l_name",l_name);
Log.i("full_name",full_name);
Log.i("profile_image",profile_image);
Log.i("gender",gender);
Log.i("email_id",email_id+"");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday, friends,albums");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
});
使用此代码我得到以下日志:
org.json.JSONException: No value for email
我确保该电子邮件是公开可用的,因为我遇到了与Google Plus登录脚本类似的问题。
答案 0 :(得分:1)
根据我上面的评论,当你得到不同的字段时:
parameters.putString("fields", "id, name, email, gender, birthday, friends, albums");
你肯定忘记了正确的权限。实际上,您可能按如下方式设置"user_profile"
:
loginButton.setReadPermissions("user_profile");
但是,当您查看the reference documentation时,您可以看到"email"
本身就是一项权限,"user_profile"
中未包含该权限。
因此,当您启动"email"
时,您必须设置不同的权限,包括LoginManager
(请参阅Add LoginButton section)。您可以将数组设置为:
loginButton = (LoginButton) view.findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("user_profile", "email", "...", "..."));
由于这个原因,您将能够检索电子邮件地址。