Login via Facebook without the app installed

Asked

Viewed 395 times

3

I have an app with a facebook login button. For integration, I followed these steps on Quickstart from android on the Facebook website. Right at the beginning it 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 feature. I’ve done the integration right by following the steps that Quickstart gives. But on my phone I’m using to debug the app, when I click on the login button gives failure, I have a textView that shows the error:

Login attempt failed

But I’ve used third-party apps that have this login functionality with facebook account but it works on my phone (the look is not the same, it does as if it were the browser), and I do not have the facebook app.

My question is: How can I log in via facebook work in my app, even if the user doesn’t have the facebook app installed?

NOTE: I am using the latest version of SDK - 2.4.

UPDATE:

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

Now I was more curious to know why when clicking the button it does not display the login dialog. Some help?

2 answers

2

To use login without the Facebook app 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 Develope and add on 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 will extend from Application, follows:

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 you have the method to display your Keyhash that you need to configure in Facebook Develope
3.2 Do this comment or delete as you will no longer need.
3.3 - Decode Facebooksdk to start in your application ("Don’t forget")
4- I created a layout where you will have 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 the login button done this will be shown in the textView a welcome message with your name.

  • I’m grateful for your willingness to help, but my code was almost the same as yours, except I’m not using it Fragment. I ended up discovering that I put the internet permission in the wrong place. But thanks for the help.

0


It’s kind of embarrassing, but I ended up putting the internet permission in the wrong place, I put inside my Mainactivity instead of outside, inside the application:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="..." >

<uses-permission android:name="android.permission.INTERNET" /> 

<application
    android:allowBackup="true"
    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>

Now the webview dialog appears normal, even without the facebook app. Thanks for your attention.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.