2016-04-20 87 views
4

我試圖從Facebook和我正在開發的應用程序獲得Facebook用戶的電子郵件。問題是我可以得到名字和其他所有的東西,但不是電子郵件。我已經在這裏查找了所有常見問題,並且我看到了很多種方法可以實現,但沒有人在我的應用上工作。我希望你能幫我一把。Android - 如何獲取用戶電子郵件與Facebook的SDK 4+

LoginActivity.java(上創建)

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     FacebookSdk.sdkInitialize(getApplicationContext()); 
     setContentView(R.layout.activity_login); 
     // Set up the login form. 

//  AppEventsLogger.activateApp(this); 
     mEmailView = (AutoCompleteTextView) findViewById(R.id.email); 
     populateAutoComplete(); 

     loginButton = (LoginButton) findViewById(R.id.login_button); 
     loginButton.setReadPermissions(Arrays.asList(
       "public_profile", "email", "user_birthday", "user_friends")); 
     // Callback registration 
     loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
      @Override 
      public void onSuccess(LoginResult loginResult) { 
       // App code 
       GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() { 
          @Override 
          public void onCompleted(JSONObject me, GraphResponse response) { 
           if (response.getError() != null) { 
            // handle error 
           } else { 
            String email = me.optString("email"); 
            String id = me.optString("id"); 
            String name = me.optString("name"); 
            Toast.makeText(getApplicationContext(), 

              email+"/"+name, 

              Toast.LENGTH_LONG).show(); 
            // send email and id to your web server 
           } 
          } 
         }).executeAsync(); 


      } 

      @Override 
      public void onCancel() { 
       // App code 
       Toast.makeText(getApplicationContext(), 

         "Cancelado", 

         Toast.LENGTH_LONG).show(); 
      } 

      @Override 
      public void onError(FacebookException exception) { 
       // App code 
       Toast.makeText(getApplicationContext(), 

         "Error", 

         Toast.LENGTH_LONG).show(); 
      } 


     }); 

//  loginButton.setReadPermissions("user_friends"); 



     mPasswordView = (EditText) findViewById(R.id.password); 
     mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
      @Override 
      public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) { 
       if (id == R.id.login || id == EditorInfo.IME_NULL) { 
        attemptLogin(); 
        return true; 
       } 
       return false; 
      } 
     }); 

     Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button); 
     mEmailSignInButton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       attemptLogin(); 
      } 
     }); 

     mLoginFormView = findViewById(R.id.login_form); 
     mProgressView = findViewById(R.id.login_progress); 
    } 

OnActivityResult

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     callbackManager.onActivityResult(requestCode, resultCode, data); 
     if (callbackManager.onActivityResult(requestCode, resultCode, data)) { 
      return; 
     } 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     profileTracker.stopTracking(); 
    } 
+1

檢查了這一點:http://stackoverflow.com/questions/29396265/i-need-users-email-address-after-successful-facebook-login-in-android- using-sdk – KishuDroid

+0

您正在測試的帳戶是否有關聯的電子郵件,並且該電子郵件是否真正被激活? – user3268305

+0

我已經試過了,它給了我一個錯誤FBuser,當然是電子郵件被激活 –

回答

5

嘗試這個

protected void connectToFacebook() { 
ArrayList<String> list = new ArrayList<String>(); 
list.add("email"); 
// LoginManager.getInstance().logInWithReadPermissions(this, list); 
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email", "user_photos", "public_profile")); 

LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
    @Override 
    public void onSuccess(LoginResult loginResult) { 
     // App code 
     GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() { 
      @Override 
      public void onCompleted(JSONObject json, GraphResponse response) { 
       // Application code 
       if (response.getError() != null) { 
        System.out.println("ERROR"); 
       } else { 
        System.out.println("Success"); 
        String jsonresult = String.valueOf(json); 
        System.out.println("JSON Result" + jsonresult); 

        String fbUserId = json.optString("id"); 
        String fbUserFirstName = json.optString("name"); 
        String fbUserEmail = json.optString("email"); 
        String fbUserProfilePics = "http://graph.facebook.com/" + fbUserId + "/picture?type=large"; 
        callApiForCheckSocialLogin(fbUserId, fbUserFirstName, fbUserEmail, fbUserProfilePics, "fb"); 
       } 
       Log.v("FaceBook Response :", response.toString()); 
      } 
     }); 
     Bundle parameters = new Bundle(); 
     parameters.putString("fields", "id,name,email,gender, birthday"); 
     request.setParameters(parameters); 
     request.executeAsync(); 


    } 

    @Override 
    public void onCancel() { 
     // App code 
     Log.v("LoginActivity", "cancel"); 
    } 

    @Override 
    public void onError(FacebookException exception) { 
     // App code 
     // Log.v("LoginActivity", "" + exception); 
     Utilities.showToast(mActivity, "" + exception); 
    } 
    }); 
} 
+0

謝謝你,真的很有幫助! –

0

創建通過GraphRequest.newMeRequest Request對象後,應setParameters與 「電子郵件」 參數的要求在執行之前,如下所示:

Bundle parameters = new Bundle(); 
parameters.putString("fields", "id,name,email"); 
request.setParameters(parameters); 
request.executeAsync(); 
相關問題