eskimoo eskimoo - 3 days ago 5
Android Question

Android Facebook 4.0.1 invalid key hash error

I have followed exactly what is told on facebook developer page for login by facebook in android. And my code works well. However When user log out and try to log in by facebook again it gives invalid key hash error. I dont know what is wrong with my code.

Manifest.xml

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:theme="@android:style/Theme.NoTitleBar" >
<activity
android:name="com.example.helltop.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".GameActivity"
android:screenOrientation="portrait"></activity>
<activity
android:name=".HelpActivity"
android:screenOrientation="portrait"></activity>
<activity android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:label="@string/app_name" />
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id" />

<meta-data
android:name="com.facebook.sdk.ApplicationName"
android:value="@string/facebook_app_name" />

<provider android:authorities="com.facebook.app.FacebookContentProvider137..."
android:name="com.facebook.FacebookContentProvider"
android:exported="true"/>

</application>


Login Activity

CallbackManager callbackManager;
LoginButton btnFbLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();

setContentView(R.layout.loginactivity);

btnFbLogin = (LoginButton) findViewById(R.id.btn_fblogin); btnFbLogin.setOnClickListener(this);btnFbLogin.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, 0, 0);

LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
fbLoginGetUserData(loginResult.getAccessToken());
}
@Override
public void onCancel() {
Toast.makeText(LoginActivity.this, "Facebook Login Canceled!" , Toast.LENGTH_SHORT).show();
}
@Override
public void onError(FacebookException error) {
Toast.makeText(LoginActivity.this, "ERROR : " + error.getMessage() , Toast.LENGTH_SHORT).show();
}
});
btnFbLogin.setReadPermissions(Arrays.asList("public_profile", "email", "user_birthday"));
profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {

}
};
}

private void fbLoginGetUserData(AccessToken accessToken) {

GraphRequest request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {

@Override
public void onCompleted(JSONObject object, GraphResponse response) {
if(response.getError() != null){
showAlert(response.getError().getErrorCode() + " : " + response.getError().getErrorMessage(), "Facebook Error!");
} else {
// Get the data here
}
}
});

Bundle parameters = new Bundle();
parameters.putString("fields", "id,first_name,last_name,link,birthday,email,gender,middle_name");
request.setParameters(parameters);
request.executeAsync();
}


Here how I created my release keystore and hash key of my app for facebook.

keytool -genkey -alias myreleasekey-keyalg RSA -keystore %USERPROFILE%\.android\myapprelease.keystore -keysize 2048 -validity 10000

keytool -exportcert -alias myreleasekey -keystore ~/myapprelease.keystore | "C:\OpenSSL\bin\openssl" sha1 -binary | "C:\OpenSSL\bin\openssl" base64

Answer
public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        printHashKey();
    }

    public void printHashKey(){

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

        } catch (NoSuchAlgorithmException e) {

        }
    }
}
Comments