0

I've this simple problem: I'm trying to implement a simple login with FB SDK 3.5 on android. I need the user to click on a normal button (no FBLoginButton) and get the email.

Until now I can handle the login with basic permissions provided by FB SDK, but I'm not able to request additional permission to the user.

I've read these answers on StackOverflow:

I've read this tutorial on FB pages:

Until now I have this code "working" ONLY for basic info (default info provided by FB).

FacebookLogin.activity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_facebook_login); 


        pref = setApplicationContext().getSharedPreferences(MainActivity.SHARED_PREFERENCES, 0); // 0 for private mode

         if (internetConnection()) {

            // start Facebook Login
            Session.openActiveSession(this, true, new Session.StatusCallback() {            

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


              if (session.isOpened()) {


                **// TRYING TO ADD NEW PERMISSIONS but doesn't work**
                Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(FacebookLogin.this, **PERMISSIONS**);
                session.requestNewReadPermissions(newPermissionsRequest);           




                // riquest /me API                
                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) { 

                      Editor pref_editor = pref.edit();

                      // token
                      pref_editor.putString(PREF_FB_ACCESS_TOKEN, session.getAccessToken());

                      // DEBUG
                      Log.i(LOG_ACTIVITY, "FB_token: "+session.getAccessToken());
                      Log.i(LOG_ACTIVITY, "FB_permissions: "+ session.getPermissions());

                      // user_id                     
                      pref_editor.putString(PREF_FB_USER_ID, user.getId());

                      // email
                      //pref_editor.putString(PREF_FB_USER_EMAIL,);


                     pref_editor.commit(); 
                  }
                }
              }).executeAsync();

                 Intent intent = new Intent(FacebookLogin.this, AfterLogin.class);
                 intent.putExtra(LOGIN_ORIGIN, "facebook");
                 startActivity(intent);
                 finish();

            }


            if (session.isClosed()){

                // do nothing.FB call to FBapp

            }         
          }
        }); 


        }       

        else {


            Toast.makeText(getApplicationContext(), "error: no internet", Toast.LENGTH_SHORT).show();


            Intent intent = new Intent(FacebookLogin.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }

Also I have the onActivityResult method inside the class/activity

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

          Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
      } 



Is there someone that knows how to fix it? Thanks in advance.

Community
  • 1
  • 1
shaolin
  • 463
  • 5
  • 13
  • Is there anything in logcat? What is the behavior you see when you ask for additional permissions? – Ming Li Nov 22 '13 at 18:43
  • I don't know where to check the information you are asking, I use logcat only to see my debug info. Also I can see the new permissions request dialog but I don't have time to press the "ok" button, then it goes to the next activity as it should do. But if I check the token with token_debugger at FB platform, I can see only the basic permissions (id,name, etc..) not the ones I asked. – shaolin Nov 24 '13 at 15:53

1 Answers1

0

Finally problem solved in this way. Hope it can be useful for someone.
Inside my activity:

private Session.StatusCallback statusCallback = new SessionStatusCallback();


onCreate() or onClick() of some button

Session.openActiveSession(MainActivity.this, true, statusCallback);


In the activiy I need to put this class:

private class SessionStatusCallback implements Session.StatusCallback{


    @Override
    public void call(Session session, SessionState state, Exception exception){
        // response to session changes
        Toast.makeText(MainActivity.this, "chiamata FB call", Toast.LENGTH_SHORT).show();

        if(session.isOpened()) {
            /// debug
            Log.i(LOG_ACTIVITY,"..session opened..ask for additional permissions");


            // additional permissions
            List<String> permissions = Arrays.asList("email, user_about_me, user_likes");
            Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(MainActivity.this, permissions);
            session.requestNewReadPermissions(newPermissionsRequest);

            //debug             
            Log.i(LOG_ACTIVITY,"..token:"+ session.getAccessToken());



            // request data
            Request.newMeRequest(session, new Request.GraphUserCallback() {

                @Override
                public void onCompleted(GraphUser user, Response response) {
                    if(user != null){
                        Toast.makeText(MainActivity.this, "ciao "+user.getName(), Toast.LENGTH_SHORT).show();
                    }

                }


            });

        }

    // check permissions
        List<String> fb_permissions = session.getPermissions();
        if ( fb_permissions.contains("email")){

            // new permission ok, go to next activity
            Intent intent = new Intent (MainActivity.this, AfterLogin.class);
            startActivity(intent);
            finish();
        }

        else{
            // display toast
            Toast.makeText(MainActivity.this, "problems with permsissions..", Toast.LENGTH_SHORT).show();
        }

    }
}

And also this method:

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



And then it works. But I still don't know how to handle the errors or if the user doesn't accept the permissions. Anyway thanks for the support and hope my work can be useful to someone.

Remember to register the activity as said in Facebook Developer's page (manifest file), and also on the facebook app filling the package and activity name fields.

shaolin
  • 463
  • 5
  • 13