1

I am trying to implement fetch user data example give on developers.facebook.com It works fine on Emulator and displayes data in logcat but when i try it in real device it doesn't open screen for login thats why it directly writes message to Logcat "Logged out"( I guess that means state is closed) following is the code

private void onSessionStateChange(Session session, SessionState state, Exception exception) {

if (state.isOpened()) {
           Log.i(TAG, "Logged in...");
        userInfoTextView.setVisibility(View.VISIBLE);

        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) {
                // Display the parsed user info
                buildUserInfoDisplay(user);
                Log.d(TAG,"Data Displayed");
            }
          }
        }).executeAsync();

    } else if (state.isClosed()) {
        Log.i(TAG, "Logged out...");
        userInfoTextView.setVisibility(View.INVISIBLE);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, 
        ViewGroup container, 
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fblogin, container, false);
    LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
    authButton.setFragment(this);
    authButton.setReadPermissions(Arrays.asList("user_likes", "user_status","user_location","email","user_birthday"));
    Log.d(TAG,"onCreateview");

    userInfoTextView = (TextView) view.findViewById(R.id.userInfoTextView);

    return view;

}

private Session.StatusCallback callback = new Session.StatusCallback() {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        onSessionStateChange(session, state, exception);
    }
};

@Override
public void onResume() {
    super.onResume();
    // For scenarios where the main activity is launched and user
    // session is not null, the session state change notification
    // may not be triggered. Trigger it if it's open/closed.
    Session session = Session.getActiveSession();
    if (session != null &&
           (session.isOpened() || session.isClosed()) ) {
        onSessionStateChange(session, session.getState(), null);
    }

    uiHelper.onResume();
}

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

@Override
public void onPause() {
    super.onPause();
    uiHelper.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
    uiHelper.onDestroy();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    uiHelper.onSaveInstanceState(outState);
}

private void buildUserInfoDisplay(GraphUser user) {
    /*if(user.getLocation()!=null){
    userInfo.append(String.format("ID: %s\n\n", 
            user.getLocation()));
    }*/
    //Log.d(TAG+" Location",user.getLocation().toString());

    Map<String, String> map;
    map = new HashMap<String, String>();

    try {
        map.put("ID", user.getId().toString());
        map.put("email", user.getProperty("email").toString());
        map.put("rel status", user.getProperty("relationship_status")
                .toString());
        map.put("name", user.getName().toString());
        // - requires user_birthday permission
        map.put("birthday", user.getBirthday().toString());
        map.put("gender", user.getProperty("gender").toString());
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
        Log.d(TAG,"Info is: "+map);
    }
}}

I have facebook app installed in device but it doesn't initiate login from that app also. do I have to add something different in order to get webview or login screen for login.

Note that this code works in emulator: Thanks for the help

Straw Hat
  • 902
  • 14
  • 38
  • Do you have Facebook app installed on the device? As that changes the way you login - through FB app vs through a WebView – Matt Gaunt Dec 09 '13 at 10:34
  • yes Facebook is installed in my device. do i need to call facebook app then? if yes then how? – Straw Hat Dec 09 '13 at 11:25
  • The facebook SDK should take care of it for you, but if you delete the FB app, does the webview method login?? If not then I'd put money on it being a configuration issue with FB API, if not then I'd have to guess that perhaps your API key hasn't propagated through their network.... – Matt Gaunt Dec 09 '13 at 11:30
  • i have deleted facebook app. after that it starts web view and I can login. but it should also work with facebook app?? – Straw Hat Dec 09 '13 at 11:43
  • Update your facebook app and it will work. – Abhishek Agarwal Dec 09 '13 at 12:01
  • facebook app is upto date :/ – Straw Hat Dec 09 '13 at 12:28
  • @AbhishekAgarwal I checked answer given below by axierjhtjz and added release key to app dashboard on facebook. but it still dont work and facebook app doesn't allow login. – Straw Hat Dec 10 '13 at 12:33

1 Answers1

0

one of the most common pitfalls of the Facebook SDK is to get the Android keyhash for the debug.keystore but not for the distribution keystore.

I asume that if your app it´s working on your emulator it´s because you have set your debug.keystore hash on your Facebook app configuration on the Facebook Developer site.

You just need to run this line on the terminal:

keytool -exportcert -alias androireleasekey -keystore PATH_TO_THE_RELEASE_KEYSTORE | openssl sha1 -binary | openssl base64

And put it on the app configuration (Facebook Developers site) next to the debug one.

Hope it helps! :)

reixa
  • 6,903
  • 6
  • 49
  • 68
  • you got the point. But I have deleted facebook app and tried to run my app again, Then webview opened and i can login.So it ran on device but using android's webview, I can't login when i have facebook app installed – Straw Hat Dec 09 '13 at 12:10
  • That´s because there´s no need for the webview login case to use the hash code. You don't even need to enter it if you're not developing a native android app. Install it again and try it after adding the new keyhash for release keystore. – reixa Dec 09 '13 at 12:15
  • Ohh! I wasn't aware of that. I will try to generate hash and try again. – Straw Hat Dec 09 '13 at 12:19
  • Also I would like to recommend you to use this awesome library that I´ve used twice https://github.com/sromku/android-simple-facebook has nothing to do with your problem but I think it will be great for you to get your Fb android code simplified. – reixa Dec 09 '13 at 14:39
  • I am new to android development so I hope you can answer few simple questions. I have generated new release keystore using eclipse because this command is not generating keystore from command prompt. Then I used your command and got the hash . can I use this hash and update it in on facebook app website or eclipse generate different hash? Correct me if I am wrong. – Straw Hat Dec 10 '13 at 05:17
  • I added the hash as mentioned in above comment. still cant login ? – Straw Hat Dec 10 '13 at 12:30
  • 2
    http://stackoverflow.com/questions/16730848/facebook-integration-in-android-application/16734424#16734424 generate hash key from this code... – Abhishek Agarwal Dec 10 '13 at 14:26
  • I recommend you to use the library that i've mentioned above. It really simplifies the fb login process. In response to what you asked on your comment, you have to generate the hash code by command line. I don't know if Eclipse does have a similar feature, either way both should be the same – reixa Dec 10 '13 at 14:55
  • @axierjhtjz Thanks for the suggestion. That Library is great, I will surely try to implement using it. – Straw Hat Dec 11 '13 at 07:06
  • @AbhishekAgarwal Thanks I got different hash from code. I added it to Facebook and logged in using app. Thanks you very much. – Straw Hat Dec 11 '13 at 08:57
  • @Vaibhav thank you please vote up the answer on the link if it helped you. – Abhishek Agarwal Dec 11 '13 at 08:59
  • @AbhishekAgarwal Sure but after my reputation reaches above 15 :D I can't vote up now :/ If you can edit and add it in this answer here I can accept it. Thats the most I can do – Straw Hat Dec 12 '13 at 03:19