B4A Library Facebook - Extends FirebaseAuth to support Facebook

This library requires B4A v12.0+

upload_2016-6-15_16-47-39.png


This library together with FirebaseAuth allows users to sign in to your app with a Facebook or Google account.

Start with configuring FirebaseAuth: https://www.b4x.com/android/forum/threads/firebaseauth-authenticate-your-users.67875/
Once it works you can follow these instructions to add support for Facebook.

You should create a facebook app with an Android platform:
SS-2016-06-15_17.06.51.png


Enable Facebook in Firebase console: Auth - Sign In Method:
SS-2016-06-15_17.04.08.png


Add the OAuth redirect URI from Firebase to Facebook Login product:
SS-2016-06-15_17.08.38.png


You will need to follow these instructions to create a hash key from B4A signing key: https://developers.facebook.com/docs/android/getting-started#release-key-hash
The alias in the command should be B4A.

Add the following snippet to the manifest editor and make sure to update facebook_app_id and the client token (https://developers.facebook.com/docs/facebook-login/guides/access-tokens#clienttokens):
B4X:
'************ Facebook Login ****************
CreateResource(values, facebook.xml, <resources>
    <string name="facebook_app_id">111111111111111</string>
   </resources>)
  
 AddApplicationText(<meta-data android:name="com.facebook.sdk.ClientToken" android:value="aaaaaaaaaabbbbbbbe053434346"/>)

AddApplicationText( <activity
            android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
           android:theme="@android:style/Theme.Translucent.NoTitleBar" />
          <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>)
  
AddApplicationText(
    <provider
            android:name="com.facebook.internal.FacebookInitProvider"
            android:authorities="${applicationId}.FacebookInitProvider"
            android:exported="false" />

        <receiver
            android:name="com.facebook.CurrentAccessTokenExpirationBroadcastReceiver"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.facebook.sdk.ACTION_CURRENT_ACCESS_TOKEN_CHANGED" />
            </intent-filter>
        </receiver>
        <receiver
            android:name="com.facebook.AuthenticationTokenManager$CurrentAuthenticationTokenChangedBroadcastReceiver"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.facebook.sdk.ACTION_CURRENT_AUTHENTICATION_TOKEN_CHANGED" />
            </intent-filter>
        </receiver>
       
         <activity
            android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:theme="@style/com_facebook_activity_theme" />
        <activity android:name="com.facebook.CustomTabMainActivity" />
        <activity
            android:name="com.facebook.CustomTabActivity"
            android:exported="true"
             >
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data
                    android:host="cct.${applicationId}"
                    android:scheme="fbconnect" />
            </intent-filter>
        </activity>
)

'************ Facebook Login (end) **********
Replace the number with your Facebook app id.

The code itself is simple. You need to initialize FacebookSdk in the starter service.
Call SignIn from an Activity to sign in.
Note that the user will sign in automatically after the first time.

Auth_SignedIn event is raised after the user signs in.


Firebase instructions: https://firebase.google.com/docs/auth/android/facebook-login
Facebook instructions: https://developers.facebook.com/docs/facebook-login/android

Note that you need to "make the app public" before other users can log in:

SS-2016-07-05_08.57.05.png



Updates

v2.01 - Adds missing dependencies.
v2.00 - Based on Facebook 14.1.1
v1.04 - Based on Facebook 11.2
v1.02 - Based on Facebook SDK 7.0.0.
v1.01 - Based on Facebook SDK 5.50.

Download link: https://www.b4x.com/android/files/facebook.zip
 

Attachments

  • FacebookExample.zip
    55.4 KB · Views: 380
Last edited:

tufanv

Expert
Licensed User
Longtime User
Can we get their facebook info also with this or just the sign in ?
 

sanjibnanda

Active Member
Licensed User
Longtime User
Download Facebook SDK: https://developers.facebook.com/docs/android/
Find a file named facebook-android-sdk-xx.xx.xx.aar in the SDK.

You need to rename it to: facebook-android-sdk.aar and copy it to the additional libraries folder.

You should create a facebook app with an Android platform:

is facebook-android-sdk.aar is deliberate extension or a mistake as compiler issue error with both .rar and .aar

with .rar

Could not find file 'C:\Program Files (x86)\Anywhere Software\Basic4android\libraries\facebook-android-sdk.aar'.

and with .aar

Generating R file. Error
Cannot find central directory

however i have completed all steps and stuck here. thanks for the updates.

the most difficult part was to get the hash key. and for me this method worked, may be helpful to some one.

http://stackoverflow.com/questions/5306009/facebook-android-generate-key-hash (use alias b4a)
 
Last edited:

sanjibnanda

Active Member
Licensed User
Longtime User
is facebook-android-sdk.aar is deliberate extension or a mistake as compiler issue error with both .rar and .aar

with .rar

Could not find file 'C:\Program Files (x86)\Anywhere Software\Basic4android\libraries\facebook-android-sdk.aar'.

and with .aar

Generating R file. Error
Cannot find central directory

however i have completed all steps and stuck here. thanks for the updates.

the most difficult part was to get the hash key. and for me this method worked, may be helpful to some one.

http://stackoverflow.com/questions/5306009/facebook-android-generate-key-hash (use alias b4a)


fixed, it works now i had put wrong files. sorry for botheration.
 

shashkiranr

Active Member
Licensed User
Longtime User
Hi All,

First time when I signin with facebook everything works and I get the user email id. But from next time onwards I am just getting these messages in the logs and nothing happens.

B4X:
sending message to waiting queue (OnActivityResult)
running waiting messages (1)

And if I check Starter.FireBaseAuth.CurrentUser.IsInitialized then its not initialized.

kindly let me know what might I be doing wrong.

Best,
SK
 

shashkiranr

Active Member
Licensed User
Longtime User
Have you tried to sign out before signing in again?

Erel I tried signing out and logging in and its not working. I removed the app from my Facebook account and again tried to log in but the same. Also I created a new Facebook app and tried, still getting the same.

Best,
SK
 

chuath

Member
Licensed User
Longtime User
I have implemented both Google and Facebook Auth successfully.
However i encounter a strange bug when both Google and Facebook are using the SAME email.

When i login to Facebook first then logout, then i try to login using Google. I would be getting the Facebook ID in the FirebaseUser.
This is fine. No problem. I can live with that.

However, IF i do the opposite something strange would happen.
I proceed with a clean state by deleting the user from Firebase console first then start the reverse process.
Google first then logout and login with Facebook. I would not be getting any ID at all.
"Auth_SignedIn" doesn't fire and i have no way to capture this error.

Is there any way to capture this so that i could inform the user to use back the Google Auth or automate it for them?

I've tried the option to enable "Multiple account per email address" under Firebase but the result would be an empty field for Email for the duplicated entry.
I needed the email entry to verify with my own user database. Therefore this method is not viable for me.
 

chuath

Member
Licensed User
Longtime User
Nothing shown for unfiltered log except this
B4X:
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
This is shown when using google signin too when everything is normal.
I think we lack an event listener for ActivityResult?
 

shashkiranr

Active Member
Licensed User
Longtime User
Hi Erel,

The below error message is from the unfiltered logs when trying to signin with facebook


B4X:
Class not found when unmarshalling: com.facebook.login.LoginClient$Request
java.lang.ClassNotFoundException: com.facebook.login.LoginClient$Request
    at java.lang.Class.classForName(Native Method)
    at java.lang.Class.forName(Class.java:309)
    at java.lang.Class.forName(Class.java:273)
    at android.os.Parcel.readParcelableCreator(Parcel.java:2281)
    at android.os.Parcel.readParcelable(Parcel.java:2245)
    at android.os.Parcel.readValue(Parcel.java:2152)
    at android.os.Parcel.readArrayMapInternal(Parcel.java:2485)
    at android.os.BaseBundle.unparcel(BaseBundle.java:221)
    at android.os.BaseBundle.getString(BaseBundle.java:918)
    at android.content.Intent.getStringExtra(Intent.java:5767)
    at com.android.server.am.ActivityStackSupervisor.startActivityLocked(ActivityStackSupervisor.java:2374)
    at com.android.server.am.ActivityStackSupervisor.startActivityMayWait(ActivityStackSupervisor.java:1857)
    at com.android.server.am.ActivityManagerService.startActivityAsUser(ActivityManagerService.java:6043)
    at com.android.server.am.ActivityManagerService.startActivity(ActivityManagerService.java:5827)
    at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:143)
    at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:4020)
    at android.os.Binder.execTransact(Binder.java:461)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.facebook.login.LoginClient$Request" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
    ... 17 more
    Suppressed: java.lang.ClassNotFoundException: com.facebook.login.LoginClient$Request
        at java.lang.Class.classForName(Native Method)
        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
        ... 18 more
    Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

Best,
SK
 
Top