2016-04-20 3 views
4

私はFacebookのユーザーメールを私が開発しているアプリから取得しようとしています。ことは、私は名前と他のすべてを得ることができますが、電子メールではありません。私はすでにここで一般的なすべての質問を探して、私はそれを行う多くの方法を見たが、誰も私のアプリで動作しません。あなたが私に手を差し伸べることを願っています。Android - facebookでユーザメールを取得する方法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();