1

I am using the following code to login to Facebook. This login process works while debugging, but does not work with signed application. I have added both debug Hash key and Release Hash key in Facebook app settings but without any success. Any idea what could be causing this?

package com.priyank.safeboard;

import java.util.Arrays;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.actionbarsherlock.app.SherlockFragment;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.widget.LoginButton;

public class MainFragment extends SherlockFragment {

    private static final String TAG = "MainFragment";
    private UiLifecycleHelper uiHelper;
    Button b;
    LoginButton authButton;

    @Override
    public View onCreateView(LayoutInflater inflater, 
            ViewGroup container, 
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.safeboardlogin, container, false);

        authButton = (LoginButton) view.findViewById(R.id.authButton);
        authButton.setFragment(this);


        /*b=(Button) view.findViewById(R.id.button1);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              updateDetail();
            }
          });*/

        authButton.setReadPermissions(Arrays.asList("user_location", "user_birthday","user_likes", "user_status"));

        return view;
    }
    protected void updateDetail() {


        Intent intent = new Intent();
        intent.setClass(getActivity(), MainActivity.class);

        startActivity(intent);


    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        uiHelper = new UiLifecycleHelper(getActivity(), callback);
        uiHelper.onCreate(savedInstanceState);


    }
    @Override
    public void onResume() {
        super.onResume();
        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 onSessionStateChange(Session session, SessionState state, Exception exception) {
        if (state.isOpened()) {
            updateDetail();
            authButton.setVisibility(View.INVISIBLE);
            //b.setVisibility(View.VISIBLE);
            Log.i(TAG, "Logged in...");
        } else if (state.isClosed()) {
            Log.i(TAG, "Logged out...");
        }
    }

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

}

I have used the following code to obtain Key hash for the app while debugging the app in log cat but how to obtain "key hash" for signed app?

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

    } catch (NoSuchAlgorithmException e) {

    }
Prione
  • 37
  • 2
  • 8

1 Answers1

1

The only thing you are changing iz application signature, so it's obvious that there is something with hash. Facebook gives wrong method for hash calculating on their pages. Today I had that problem too - this solution: Generating hash key for app using facebook sdk works ok for me.

Community
  • 1
  • 1
piotrpo
  • 12,398
  • 7
  • 42
  • 58
  • I have already used this and was prompted for the password and got the hash key but still not able to loin in my release version of the app: : C:\Program Files\Java\jre7\bin>keytool -exportcert -alias signedkeyalias -keystore "C:\Users\priyankmandalia\desktop\Keystorename"| "C:\Openssl\bin\openssl"sha1 -binary |"C:\Openssl\bin\openssl"base64 – Prione Aug 08 '13 at 05:10
  • The linked answer calculate hash just in the way facebook does, so it seems to be most reliable. What is the error you have got? – piotrpo Aug 08 '13 at 07:10
  • I already have correct debug hash tag and its working correctly but i am not able to login in the signed app having release hash tag. So how to obtain correct signed app hash tag? – Prione Aug 08 '13 at 12:17
  • I have used this code and obtained the key hash of my debug app. But how to obtain key hash for signed app:try { PackageInfo info = getPackageManager().getPackageInfo( "Your package name", PackageManager.GET_SIGNATURES); for (Signature signature : info.signatures) { MessageDigest md = MessageDigest.getInstance("SHA"); md.update(signature.toByteArray()); Log.d("Your Tag", Base64.encodeToString(md.digest(), Base64.DEFAULT)); }} catch (NameNotFoundException e) {} catch (NoSuchAlgorithmException e) { } – Prione Aug 09 '13 at 07:56
  • Also do i need to submit App details for review to make it work with signed app for releasing in Google Play? – Prione Aug 09 '13 at 08:04
  • Axactly this same way, but sign the app with your release key, not the debug one. – piotrpo Aug 09 '13 at 08:07
  • I am getting rno3QHFKy+h************Fwbw= key hash while debugging the app but then after using Export tool debug apk turns into signed apk. But then how to obtain key hash for Signed app? – Prione Aug 09 '13 at 08:16
  • Debug apk is working correctly and i am login into my app but i am not able to login in the signed apk for releasing it on Google Play. Do you have solution for that? – Prione Aug 09 '13 at 18:27