简体   繁体   中英

Login with facebook sdk 3.5 and get email - android

I'm new to android development and also new here on SO. My question is : I have a button (not the facebook LogginButton) that implements the facebook login. I also need, after login, to get the user's email. But the way i've implemented it turns out that sometimes it doesn't retrieve the user's mail. If its the first time i'm using the app i have to click several times (at least twice) on login button of my app so it finally brings me the email and after the first time i get the email, it will always bring me the email next time i click on login button. But, if i go on my fb acount and remove the app , the problem shows up again. I think i'm having some problem with settin the permission or with managing sessions. I'm gonna post my code below:

    public void onClickLoginFacebook(View view) {
Session.StatusCallback statusCallBack =  new Session.StatusCallback() {

// callback when session changes state
@Override
public void call(Session session, SessionState state, Exception exception) {

    if (session.isOpened()) {
         // make request to the /me API
             Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {

                  // callback after Graph API response with user object
                  @Override
              public void onCompleted(GraphUser user, Response response) {

                    if (user != null) {
                        if (user.asMap().get("email")!=null){
                        userFacebook.setEmail(user.asMap().get("email").toString());
                     }


                }else{
                        Toast.makeText(getApplicationContext(), "Error on facebook login: " + response.getError().getErrorMessage(), Toast.LENGTH_LONG).show();
                    }

            }

            });
          }else if(session.isClosed()){
              Toast.makeText(getApplicationContext(), "Error on facebook login", Toast.LENGTH_LONG).show();
          }

}
    };

    if( isOnline() )
    {

        Session session = Session.getActiveSession();
        if(session == null){
            session = new Session.Builder(this).setApplicationId(
                            this.getString(R.string.app_id)).build();
                           Session.setActiveSession(session);
        }
            if (!session.isOpened() && !session.isClosed()) {
                    session.openForRead(new Session.OpenRequest(this)
                        .setPermissions(Arrays.asList("email"))
                        .setCallback(statusCallBack));
            } else {
                    Session.openActiveSession(LoginActivity.this, true, statusCallBack);
            }

    }
    else
    {
            Toast.makeText(getApplicationContext(), R.string.error_message, Toast.LENGTH_LONG).show();
    }
  }


      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data);
 Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}

Could anyone please help me? PS.: Sorry for my poor English =/

Before calling Session.openActiveSession() do this to get permissions add this:

List<String> permissions = new ArrayList<String>();
permissions.add("email");

The last parameter in Session.openActiveSession() should be permissions. Now you can access user.getProperty("email").toString() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM