我爲我的android應用使用了Facebook sdk 4.2。但我無法獲得我的電子郵件,並且它對電子郵件沒有任何價值。不過,我可以得到其他的價值,如姓名,鏈接,身份證,性別。關於這個從stackoverflow有一些話題,但他們正在使用舊的sdk版本,類是不同的。請幫忙,這是我的代碼。Android Facebook sdk 4.2無法獲取電子郵件
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:否值電子郵件 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 $ 2 $ 1.onCompleted(MainActivity.java:242) 06-05 13:47:56.254:W/System.err(9965 ):at com.facebook.GraphRequest $ 1.onCompleted(GraphRequest.java:295) 06-05 13:47:56.254:W/System.err(9965):at com.facebook.GraphRequest $ 5.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):在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)
我添加此LoginManager.logInWithReadPermissions(這一點,Arrays.asList( 「public_profile」, 「電子郵件」));但它stil不會工程.. – KLAng
你有沒有嘗試過這對多個用戶?你確定你正在測試的用戶有一個與他們的帳戶相關的電子郵件地址嗎? – user3268305
解決。我不知道我做了什麼來解決,但這個許可肯定會幫助一些。謝謝。 – KLAng