0

I am facing problem in Facebook login in android,i have create key hash using method that facebook provide and added it to facebook developers console. I have updated my manifest-

 <uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
 <meta-data 
     android:name="com.facebook.sdk.ApplicationId" 
     android:value="@string/fb_app_id"/>
 <activity android:name="com.facebook.LoginActivity" 
               android:screenOrientation="portrait">

and below is my code in activity -

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();
    Session session = Session.getActiveSession();
    if(session != null && (session.isOpened() || session.isClosed())) {
        onSessionStateChange(session, session.getState(), null);
    }
    else{
        if(isFbClick){
            Session s = new Session(mContext);
            Session.setActiveSession(s);
            s.openForRead(new Session.OpenRequest(this).setCallback(callback).setPermissions(Arrays.asList("basic_info","email")));
        }
    }
    uiHelper.onResume();
}

@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);
}

   @SuppressWarnings("deprecation")
private void onSessionStateChange(final Session session, SessionState state, Exception exception) {
    flag = true;
    if(session.isClosed()){
        state  = SessionState.CLOSED;
    }
    if (session.isOpened() && state.isOpened()) {
            accessToken = session.getAccessToken();
            accessExpir = session.getExpirationDate();
            Log.i(TAG, "Logged in...");
            if (state.isOpened()) {
                if(mDialog==null){
                    mDialog = new ProgressDialog(mContext);
                }
                if(!mDialog.isShowing()){
                    mDialog.setMessage("please wait...");
                    mDialog.setCancelable(false);
                    mDialog.show();
                }
                 Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
                     public void onCompleted(GraphUser user, Response response) {
                          session.close();
                          session.removeCallback(callback);
                          getUserFacebookData(user);
                     }
                 });
             }
    }else if(state.equals(SessionState.CLOSED_LOGIN_FAILED)){

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


 @Override
protected void onStart() {
    super.onStart();
     Session session = Session.getActiveSession();
     if(session.isOpened()){
         session.close();
         session = null;
     }
}

and code inside my oncreate is -

     uiHelper = new UiLifecycleHelper(this, callback);
     uiHelper.onCreate(savedInstanceState);
     authButton = (LoginButton) findViewById(R.id.authButton);
        authButton.setReadPermissions(Arrays.asList("basic_info","email"));
        authButton.setBackgroundResource(R.drawable.fb_login);
        authButton.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0,0);
        authButton.setText("");
        authButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                isFbClick = true;
                Session s = new Session(mContext);
                Session.setActiveSession(s);
                s.openForRead(new Session.OpenRequest((Activity) mContext).setCallback(callback).setPermissions(Arrays.asList("public_profile","email")));
            }
        });

at the end when i click facebook button it returns error after user login - Session state:CLOSED_LOGIN_FAILED. I have try it on both Mobile(with facebook app installed and not installed) and emulator.

Thanks in advance.

Ravi Bhandari
  • 4,682
  • 8
  • 40
  • 68

2 Answers2

1

Have you added the keyHash on developers.facebook.com for your application?

Check it out. There is a way how to obtain your keyHash. https://stackoverflow.com/a/15287332/3025055

Community
  • 1
  • 1
PaintedRed
  • 1,398
  • 5
  • 19
  • 30
  • thanks for your reply. I have all ready used this code to generate key hash and also try using command prompt both generate same key hash but while login through facebook it gives error. – Ravi Bhandari Jul 30 '14 at 14:09
  • Thank you - this was my problem – Kibi Jan 19 '15 at 10:14
0

This may not be the problem but it is a common problem (it was for me when I implemented Facebook). Chances are you have the wrong hash key. Put this in your onCreate method:

try {
PackageInfo info = getPackageManager().getPackageInfo(
        "com.example.com.tvishi.fb", 
        PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(signature.toByteArray());
    Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
    }
} catch (NameNotFoundException e) {

} catch (NoSuchAlgorithmException e) {

}

This will Log the hashkey that Facebook is trying to use. If you copy that hashkey into your Facebook developer's console, it may solve your problem.

user1282637
  • 1,827
  • 5
  • 27
  • 56
  • thanks for your reply. I have all ready used this code to generate key hash and also try using command prompt both generate same key hash but while login through facebook it gives error. – Ravi Bhandari Jul 30 '14 at 14:08
  • no, it doent gives any error.When user click facebook button then after few delay onActivity result called and then onSessionState shoe Login Failed session closed status. – Ravi Bhandari Jul 30 '14 at 14:34