我获准从facebook帐户收到电子邮件。虽然我无法得到它。它总是一个空字符串。 这是我的代码。请帮帮我。
我正在添加我使用的代码。它是从stackoverflow复制的。
fbLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this, Arrays.asList("public_profile","email","basic_info"));
}
});
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest.newMeRequest(
loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject user, GraphResponse response) {
if (response.getError() != null) {
// handle error
} else {
socialData = new ArrayList<NameValuePair>();
String email = user.optString("email");
String id = user.optString("id");
String name = user.optString("name");
socialData.add(new BasicNameValuePair("id",id));
socialData.add(new BasicNameValuePair("name",name));
socialData.add(new BasicNameValuePair("email",email));
Log.e("id",id);
Log.e("name",name);
Log.e("email",email);
Log.e("object",user.toString());
// send email and id to your web server
SocialLogin socialLogin = new SocialLogin();
socialLogin.execute("app/social-login");
}
}
}).executeAsync();
}
@Override
public void onCancel() {
Toast.makeText(LoginActivity.this, "Login Cancel", Toast.LENGTH_LONG).show();
}
@Override
public void onError(FacebookException exception) {
Toast.makeText(LoginActivity.this, exception.getMessage(), Toast.LENGTH_LONG).show();
}
});
这是我的输出
E/id﹕ 1046433868724117
E/name﹕ Jafer Poovan P
E/email﹕ '
答案 0 :(得分:2)
查看v2.4的更改日志中的“声明字段”:https://developers.facebook.com/docs/apps/changelog#v2_4
您现在必须在API调用中指定字段,例如:/me?fields=name,email
...否则你只会得到字段“id”和“name”。
答案 1 :(得分:0)
尝试使用此代码。它的成功运作。
loginButton = (LoginButton) findViewById(R.id.login_button);
List < String > permissionNeeds = Arrays.asList("user_photos", "email",
"user_birthday", "public_profile", "AccessToken");
loginButton.registerCallback(callbackManager,
new FacebookCallback < LoginResult > () {@Override
public void onSuccess(LoginResult loginResult) {
System.out.println("onSuccess");
String accessToken = loginResult.getAccessToken()
.getToken();
Log.i("accessToken", accessToken);
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {@Override
public void onCompleted(JSONObject object,
GraphResponse response) {
Log.i("LoginActivity", response.toString());
try {
id = object.getString("id");
try {
URL profile_pic = new URL(
"http://graph.facebook.com/" + id + "/picture?type=large");
Log.i("profile_pic",
profile_pic + "");
} catch (MalformedURLException e) {
e.printStackTrace();
}
name = object.getString("name");
email = object.getString("email");
gender = object.getString("gender");
birthday = object.getString("birthday");
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields",
"id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
System.out.println("onCancel");
}
@Override
public void onError(FacebookException exception) {
System.out.println("onError");
Log.v("LoginActivity", exception.getCause().toString());
}
});
&#13;
了解更多信息和完整的Facebook登录代码 Check my answer HERE