我目前正在创建一个测试应用程序来测试使用最新的 facebook SDK 来更新我们现有的应用程序问题是我需要获取我知道的电子邮件地址,这取决于用户是否在他的帐户中提供了一个。现在我用来测试的帐户确实提供了一个,但由于未知原因,facebook SDK 只提供了 user_id 和帐户的全名,没有别的。我对此感到困惑,因为 SDK3 及更高版本提供了比更新的 SDK4 更多的信息,而且我不知道如何获取电子邮件,因为到目前为止我看到的所有答案都没有提供我的电子邮件。到目前为止,这是我的代码:
登录按钮
@OnClick(R.id.btn_login)
public void loginFacebook(){
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "email"));
}
登录管理器回调:
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
requestUserProfile(loginResult);
}
@Override
public void onCancel() {
Toast.makeText(getBaseContext(),"Login Cancelled", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(FacebookException e) {
Toast.makeText(getBaseContext(),"Problem connecting to Facebook", Toast.LENGTH_SHORT).show();
}
});
以及对用户个人资料的请求:
public void requestUserProfile(LoginResult loginResult){
GraphRequest.newMeRequest(
loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject me, GraphResponse response) {
if (response.getError() != null) {
// handle error
} else {
try {
String email = response.getJSONObject().get("email").toString();
Log.e("Result", email);
} catch (JSONException e) {
e.printStackTrace();
}
String id = me.optString("id");
// send email and id to your web server
Log.e("Result1", response.getRawResponse());
Log.e("Result", me.toString());
}
}
}).executeAsync();
}
JSON 响应仅返回我的帐户的 ID 和全名,但不包括电子邮件。我错过了什么吗?