Login via Facebook without the application installed

3

I have an app with a facebook login button. For integration, I followed these steps on the android quickstart on the facebook developers site. Well 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 that I have to install the app to use the login functionality. I've done the right integration following the steps that quickstart gives. But on my phone that I'm using to debug the app, when I click the login button it crashes, 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 facebook account but it works on my phone (the look is not the same, it does as it is through the browser), and I do not have the facebook app .

My question is: How can I sign in to facebook to work in my app, even if the user does not have the facebook application installed?

I'm using the latest version of SDK - 2.4.

UPDATE:

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

Now I'm more curious to know why clicking the button does not display the login dialog. Any help?

    
asked by anonymous 23.09.2015 / 20:41

2 answers

0

It's kind of embarrassing, but I ended up putting the internet permission in the wrong place, put it 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. Thank you for your attention.

    
24.09.2015 / 13:27
2

To use login without the Facebook application installed, you have to add an Acitivity in 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 did mine.
1- Create an Facebook App Develope and add in gradle

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

2- Add to your AndroidManifest within 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 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 has the method to display your KeyHash that you need to configure in Facebook Develope
3.2 done this comment or delete because you will not need more. 3.3 - Disable FacebookSdk to start in your application ("Do not forget")
4- I created a layout where it 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- Have your MainActivity call the Fragment.

6.1 - The user will click the login button as this will be shown in the textView a welcome message with your name.

    
23.09.2015 / 22:48