Tom Kincaid
Tom Kincaid

Reputation: 4975

Android Facebook SDK Not Working for Exported APK

I have a very basic Android app using the Facebook SDK based on the example at https://developers.facebook.com/docs/android/getting-started/facebook-sdk-for-android/

If I run it from ADT, it works and shows "opened". Since I have already authorized the app, it doesn't show a dialog and just displays the text "opened".

If I export it as an apk and sign it with a new keystore that I just created, it works the same.

However, if I export an apk and sign it with my actual keystore that I have used to sign my real apps, it doesn't work. First, it shows a dialog to access my public profile and friends list even though I have already authorized it, then after clicking OK it shows "closed" meaning session.isClosed() was true.

I have all the key hashes of the debug, new, and actual keystore in the Facebook developer tool.

What could be wrong? Is my keystore hosed?

Here is the code of my app:

package com.example.newtest;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.TextView;
import com.facebook.*;
import com.facebook.model.*;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Session.openActiveSession(this, true, new Session.StatusCallback() {

            @Override
            public void call(Session session, SessionState state, Exception exception) {

                if (session.isOpened()) {

                    TextView welcome = (TextView) findViewById(R.id.welcome);
                    welcome.setText("opened");

                } else if (session.isClosed()) {

                    TextView welcome = (TextView) findViewById(R.id.welcome);
                    welcome.setText("closed");

                } else {

                    TextView welcome = (TextView) findViewById(R.id.welcome);
                    welcome.setText("neither opened nor closed");
                }
            }
        });
    }

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

}

Upvotes: 0

Views: 1502

Answers (1)

Emmanuel
Emmanuel

Reputation: 13223

Facebook's command line method to generate the keyhash it is not always reliable. Use this code in onCreate() to make sure that you are generating the correct key:

 try {
        PackageInfo info = getPackageManager().getPackageInfo(
                "package.name.here", 
                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) {

    }

Make sure you replace "package.name.here" with your actual package name.

Upvotes: 2

Related Questions