android – Login via Facebook without the app installed

Question:

I have an app with a login button for facebook. For integration, I followed these steps in android quickstart on facebook developers site. At the very beginning he says:

Facebook SDK for Android uses Facebook to support Login and Sharing. If you develop or test with a real device, install Facebook from Google Play.

Saying I have to install the app to use the login functionality. I've done the integration right following the steps that quickstart gives. But on my cell phone that I'm using to debug the app, when I click on the login button it fails, I have a textView that shows the error:

Login attempt failed

But I've already used third-party apps that have this login functionality with a facebook account but it works on my cell phone (the look isn't the same, it does as if it were in the browser), and I don't have the facebook app.

My question is: How can I login through facebook work in my app even if the user doesn't have the facebook app installed?

NOTE: I'm using the latest SDK version – 2.4.

UPDATE:

The documentation says that if the facebook application is installed, the native dialog will be displayed; if not installed, a non-native dialog will be displayed in a webview.

Now I'm more curious to know why clicking the button doesn't display the login dialog. Some help?

Answer:

To use the login without the Facebook application installed, you have to add an Acitivity to your AndroidManifest, follow the code:

 <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" />

I'll show you how I made mine.
1- Create an App on Facebook Develop and add it to gradle

compile 'com.facebook.android:facebook-android-sdk:4.5.0'

2- Add to your AndroidManifest inside the application tag

<application
    android:allowBackup="true"
    android:name=".MyApplication"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

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

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </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" />

</application>

3- Create a Class that extends from Application, follow:

public class MyApplication extends Application {
@Override
public void onCreate() {
    super.onCreate();
    printKeyHash();
    //FacebookSdk.sdkInitialize(getApplicationContext()); // para iniciar o FacebookSDK
}
public void printKeyHash() {
    try {
        PackageInfo info = getPackageManager().getPackageInfo("com.examplo.blabla", PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.e("TAG", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (PackageManager.NameNotFoundException e) {

    } catch (NoSuchAlgorithmException e) {

    }
}
}

3.1- In my class there is a method to display your KeyHash that you need to configure in Facebook Develop
3.2 Once that's done, comment or delete it as you won't need it anymore.
3.3 – Uncomment FacebookSdk to start in your application ("Don't forget")
4- I created a layout where there will be a button and a textView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainFragment">

<TextView
    android:id="@+id/text_details"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

<com.facebook.login.widget.LoginButton
    android:id="@+id/login_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="192dp" />

</RelativeLayout>

5- And a Fragment that will contain:

public class MainFragment extends Fragment {

private TextView mTextDetails;
private CallbackManager mCallbackManager;
private AccessTokenTracker mTokenTracker;
private ProfileTracker mProfileTracker;

private FacebookCallback<LoginResult> mFacebookCallback = new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {
        Log.d("TAG", "onSuccess");
        AccessToken accessToken = loginResult.getAccessToken();

        Profile profile = Profile.getCurrentProfile();

        mTextDetails.setText(constructWelcomeMessage(profile));

    }


    @Override
    public void onCancel() {
        Log.d("TAG", "onCancel");
    }

    @Override
    public void onError(FacebookException e) {
        Log.d("TAG", "onError " + e);
    }
};


public MainFragment() {
}

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

    mCallbackManager = CallbackManager.Factory.create();
    setupTokenTracker();
    setupProfileTracker();

    mTokenTracker.startTracking();
    mProfileTracker.startTracking();


}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_main, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    setupTextDetails(view);
    setupLoginButton(view);
}

@Override
public void onResume() {
    super.onResume();
    Profile profile = Profile.getCurrentProfile();
    mTextDetails.setText(constructWelcomeMessage(profile));
}

@Override
public void onStop() {
    super.onStop();
    mTokenTracker.stopTracking();
    mProfileTracker.stopTracking();
}

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

private void setupTextDetails(View view) {
    mTextDetails = (TextView) view.findViewById(R.id.text_details);
}


private void setupTokenTracker() {
    mTokenTracker = new AccessTokenTracker() {
        @Override
        protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) {
            Log.d("TAG", "" + currentAccessToken);
        }
    };
}

private void setupProfileTracker() {
    mProfileTracker = new ProfileTracker() {
        @Override
        protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
            Log.d("TAG", "" + currentProfile);
            mTextDetails.setText(constructWelcomeMessage(currentProfile));
        }
    };
}

private void setupLoginButton(View view) {
    LoginButton mButtonLogin = (LoginButton) view.findViewById(R.id.login_button);
    mButtonLogin.setFragment(this);
    mButtonLogin.setCompoundDrawables(null, null, null, null);
    mButtonLogin.setReadPermissions("user_friends");
    mButtonLogin.registerCallback(mCallbackManager, mFacebookCallback);
}

private String constructWelcomeMessage(Profile profile) {
    StringBuffer stringBuffer = new StringBuffer();
    if (profile != null) {
        stringBuffer.append("Bem - vindo " + profile.getName());
    }
    return stringBuffer.toString();
}

}

6- Make your MainActivity call Fragment.

6.1 – The user will click on the login button done this will be shown in the textView a welcome message with their name.

Scroll to Top