Android Facebook sdk 4.2 无法获取邮件
Android Facebook sdk 4.2 cannot get email
我的 android 应用程序使用 facebook sdk 4.2。但是我无法收到我的电子邮件,而且它没有显示电子邮件的价值。但是,我可以获得其他值,例如名称、link、ID、性别。 Whosebug 中有一些关于此的主题,但他们使用的是较旧的 sdk 版本,class 不同。请帮忙,这是我的代码。
FacebookSdk.sdkInitialize(this.getApplicationContext());
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback < LoginResult > () {
@Override
public void onSuccess(LoginResult loginResult) {
Toast.makeText(MainActivity.this, "SUCCESS", Toast.LENGTH_SHORT).show();
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
try {
String id = object.getString("id");
String name = object.getString("name");
String email = object.getString("email"); //Get null value here
String gender = object.getString("gender");
Toast.makeText(MainActivity.this, "HI," + name + "Gender: " + gender, Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
我的日志文件
06-05 13:47:56.207: W/System.err(9965): org.json.JSONException: No value for email
06-05 13:47:56.246: W/System.err(9965): at org.json.JSONObject.get(JSONObject.java:355)
06-05 13:47:56.246: W/System.err(9965): at org.json.JSONObject.getString(JSONObject.java:515)
06-05 13:47:56.254: W/System.err(9965): at com.android.jobstreet.MainActivity.onCompleted(MainActivity.java:242)
06-05 13:47:56.254: W/System.err(9965): at com.facebook.GraphRequest.onCompleted(GraphRequest.java:295)
06-05 13:47:56.254: W/System.err(9965): at com.facebook.GraphRequest.run(GraphRequest.java:1243)
06-05 13:47:56.254: W/System.err(9965): at android.os.Handler.handleCallback(Handler.java:733)
06-05 13:47:56.254: W/System.err(9965): at android.os.Handler.dispatchMessage(Handler.java:95)
06-05 13:47:56.254: W/System.err(9965): at android.os.Looper.loop(Looper.java:136)
06-05 13:47:56.254: W/System.err(9965): at android.app.ActivityThread.main(ActivityThread.java:5146)
06-05 13:47:56.254: W/System.err(9965): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 13:47:56.254: W/System.err(9965): at java.lang.reflect.Method.invoke(Method.java:515)
06-05 13:47:56.254: W/System.err(9965): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
06-05 13:47:56.254: W/System.err(9965): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
06-05 13:47:56.254: W/System.err(9965): at dalvik.system.NativeStart.main(Native Method)
用户登录时必须申请"email"权限。即使你的应用有邮箱权限,也不会总是return邮箱地址,所以你的应用不应该依赖在那个字段上总是有一个值。
更多信息来自 Facebook documentation。
您必须设置以下权限
loginbtn.setReadPermissions("email");
我的 android 应用程序使用 facebook sdk 4.2。但是我无法收到我的电子邮件,而且它没有显示电子邮件的价值。但是,我可以获得其他值,例如名称、link、ID、性别。 Whosebug 中有一些关于此的主题,但他们使用的是较旧的 sdk 版本,class 不同。请帮忙,这是我的代码。
FacebookSdk.sdkInitialize(this.getApplicationContext());
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback < LoginResult > () {
@Override
public void onSuccess(LoginResult loginResult) {
Toast.makeText(MainActivity.this, "SUCCESS", Toast.LENGTH_SHORT).show();
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
try {
String id = object.getString("id");
String name = object.getString("name");
String email = object.getString("email"); //Get null value here
String gender = object.getString("gender");
Toast.makeText(MainActivity.this, "HI," + name + "Gender: " + gender, Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
我的日志文件
06-05 13:47:56.207: W/System.err(9965): org.json.JSONException: No value for email 06-05 13:47:56.246: W/System.err(9965): at org.json.JSONObject.get(JSONObject.java:355) 06-05 13:47:56.246: W/System.err(9965): at org.json.JSONObject.getString(JSONObject.java:515) 06-05 13:47:56.254: W/System.err(9965): at com.android.jobstreet.MainActivity.onCompleted(MainActivity.java:242) 06-05 13:47:56.254: W/System.err(9965): at com.facebook.GraphRequest.onCompleted(GraphRequest.java:295) 06-05 13:47:56.254: W/System.err(9965): at com.facebook.GraphRequest.run(GraphRequest.java:1243) 06-05 13:47:56.254: W/System.err(9965): at android.os.Handler.handleCallback(Handler.java:733) 06-05 13:47:56.254: W/System.err(9965): at android.os.Handler.dispatchMessage(Handler.java:95) 06-05 13:47:56.254: W/System.err(9965): at android.os.Looper.loop(Looper.java:136) 06-05 13:47:56.254: W/System.err(9965): at android.app.ActivityThread.main(ActivityThread.java:5146) 06-05 13:47:56.254: W/System.err(9965): at java.lang.reflect.Method.invokeNative(Native Method) 06-05 13:47:56.254: W/System.err(9965): at java.lang.reflect.Method.invoke(Method.java:515) 06-05 13:47:56.254: W/System.err(9965): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732) 06-05 13:47:56.254: W/System.err(9965): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566) 06-05 13:47:56.254: W/System.err(9965): at dalvik.system.NativeStart.main(Native Method)
用户登录时必须申请"email"权限。即使你的应用有邮箱权限,也不会总是return邮箱地址,所以你的应用不应该依赖在那个字段上总是有一个值。
更多信息来自 Facebook documentation。
您必须设置以下权限
loginbtn.setReadPermissions("email");