Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate facebook login into android app

I am trying to integrate facebook login into my app

I went over this tutorial : https://developers.facebook.com/docs/android/getting-started/facebook-sdk-for-android/

downloaded facebook sdk 3.5

step by step - downloaded openssl , created an androidkeystore , generated hashkey, created an app in facebook development console, gave it my package name, and the activity that logs in, and the hashcode printed to me by the device in the log console, as the tutorial suggested + the hashkey i generated with openssl, added the app_id to the strings file and the required permissions activities and metadata to the android manifest file

now i opened the app and clicked the "login with facebook button" it asked for my permission to user profile, i clicked ok

and then the log printed this exception :

10-16 19:51:20.718: W/Bundle(8444): Key com.facebook.platform.protocol.PROTOCOL_VERSION expected String but value was a java.lang.Integer.  The default value <null> was returned.
10-16 19:51:20.718: W/Bundle(8444): Attempt to cast generated internal exception:
10-16 19:51:20.718: W/Bundle(8444): java.lang.ClassCastException: java.lang.Integer
10-16 19:51:20.718: W/Bundle(8444):     at android.os.Bundle.getString(Bundle.java:1040)
10-16 19:51:20.718: W/Bundle(8444):     at android.content.Intent.getStringExtra(Intent.java:3412)
10-16 19:51:20.718: W/Bundle(8444):     at com.facebook.AuthorizationClient$KatanaLoginDialogAuthHandler.tryAuthorize(AuthorizationClient.java:829)
10-16 19:51:20.718: W/Bundle(8444):     at com.facebook.AuthorizationClient.tryCurrentHandler(AuthorizationClient.java:278)
10-16 19:51:20.718: W/Bundle(8444):     at com.facebook.AuthorizationClient.tryNextHandler(AuthorizationClient.java:244)
10-16 19:51:20.718: W/Bundle(8444):     at com.facebook.AuthorizationClient$GetTokenAuthHandler.getTokenCompleted(AuthorizationClient.java:778)
10-16 19:51:20.718: W/Bundle(8444):     at com.facebook.AuthorizationClient$GetTokenAuthHandler$1.completed(AuthorizationClient.java:737)
10-16 19:51:20.718: W/Bundle(8444):     at com.facebook.internal.PlatformServiceClient.callback(PlatformServiceClient.java:144)
10-16 19:51:20.718: W/Bundle(8444):     at com.facebook.internal.PlatformServiceClient.handleMessage(PlatformServiceClient.java:128)
10-16 19:51:20.718: W/Bundle(8444):     at com.facebook.internal.PlatformServiceClient$1.handleMessage(PlatformServiceClient.java:54)
10-16 19:51:20.718: W/Bundle(8444):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-16 19:51:20.718: W/Bundle(8444):     at android.os.Looper.loop(Looper.java:130)
10-16 19:51:20.718: W/Bundle(8444):     at android.app.ActivityThread.main(ActivityThread.java:3906)
10-16 19:51:20.718: W/Bundle(8444):     at java.lang.reflect.Method.invokeNative(Native Method)
10-16 19:51:20.718: W/Bundle(8444):     at java.lang.reflect.Method.invoke(Method.java:507)
10-16 19:51:20.718: W/Bundle(8444):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:840)
10-16 19:51:20.718: W/Bundle(8444):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:598)
10-16 19:51:20.718: W/Bundle(8444):     at dalvik.system.NativeStart.main(Native Method)

which is a warning, the app didn't crash because of it, but the login itself fails

this is my code for the login flow:

 import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;

public class MainActivity extends FragmentActivity {

private static final int SPLASH = 0;
private static final int SELECTION = 1;
private static final int FRAGMENT_COUNT = SELECTION + 1;

private Fragment[] fragments = new Fragment[FRAGMENT_COUNT];

private boolean isResumed = false;

private UiLifecycleHelper uiHelper;
private Session.StatusCallback callback = 
    new Session.StatusCallback() {
    @Override
    public void call(Session session, 
            SessionState state, Exception exception) {
        onSessionStateChange(session, state, exception);
    }
};
/**
 * @param savedInstanceState
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    uiHelper = new UiLifecycleHelper(this, callback);
    uiHelper.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentManager fm = getSupportFragmentManager();
    fragments[SPLASH] = fm.findFragmentById(R.id.splashFragment);
    fragments[SELECTION] = fm.findFragmentById(R.id.selectionFragment);

    FragmentTransaction transaction = fm.beginTransaction();
    for (int i = 0; i < fragments.length; i++) {
        transaction.hide(fragments[i]);
    }
    transaction.commit();
}


private void showFragment(int fragmentIndex, boolean addToBackStack) {
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction transaction = fm.beginTransaction();
    for (int i = 0; i < fragments.length; i++) {
        if (i == fragmentIndex) {
            transaction.show(fragments[i]);
        } else {
            transaction.hide(fragments[i]);
        }
    }
    if (addToBackStack) {
        transaction.addToBackStack(null);
    }
    transaction.commit();
}

@Override
public void onResume() {
    super.onResume();
    uiHelper.onResume();
    isResumed = true;
}

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

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

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

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

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    // Only make changes if the activity is visible
    if (isResumed) {
        FragmentManager manager = getSupportFragmentManager();
        // Get the number of entries in the back stack
        int backStackSize = manager.getBackStackEntryCount();
        // Clear the back stack
        for (int i = 0; i < backStackSize; i++) {
            manager.popBackStack();
        }
        if (state.isOpened()) {
            // If the session state is open:
            // Show the authenticated fragment
            showFragment(SELECTION, false);
        } else if (state.isClosed()) {
            // If the session state is closed:
            // Show the login fragment
            showFragment(SPLASH, false);
        }
    }
}

@Override
protected void onResumeFragments() {
    super.onResumeFragments();
    Session session = Session.getActiveSession();

    if (session != null && session.isOpened()) {
        // if the session is already open,
        // try to show the selection fragment
        showFragment(SELECTION, false);
    } else {
        // otherwise present the splash screen
        // and ask the person to login.
        showFragment(SPLASH, false);
    }
}

}

What am i doing wrong? why doesnt it work ?

like image 766
Lena Bru Avatar asked Jan 30 '26 17:01

Lena Bru


1 Answers

The problem was -

nowhere on the tutorial page did it say that i MUST exit the sandbox mode if i am trying to login with an account that is not the admin of the facebook app or one of the defined fake accounts

once i put the app into live mode, it worked

like image 156
Lena Bru Avatar answered Feb 02 '26 07:02

Lena Bru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!