B4A Library Facebook

This library will allow your apps to interact with Facebook.


Requirements:

1- B4A 3.x

2- Facebook account.

3- Copy the Facebook.xml and Facebook.jar to your additional libraries directory.


Usage:
B4X:
'Initialize the library
Dim FB As Facebook

'Get the following 3 fields from Facebook after creating a Facebook app.
Dim AppID As String = "<APP_ID_FROM_FACEBOOK>"
Dim AppSecret As String = "<APP_SECRET_FROM_FACEBOOK>"
Dim myWebsite As String = "<LINK_TO_THE_WEBSITE_IN_REDIRECT_URI>"

FB.Initialize(Activity, Me, "myFB", AppID, AppSecret, myWebsite)

'Login to Facebook
FB.Login

'Get your profile
FB.GetMyProfile

...

'Display profile fields
Sub myFB_MyProfile_Response(Profile As Map)

    Dim Temp As String

    For I = 0 To Profile.Size - 1

        Temp = Temp & Profile.GetKeyAt(I) & ": " & Profile.GetValueAt(I) & CRLF

    Next

    Msgbox(Temp, "My Profile")


End Sub

The attached files contain:

a- The Facebook library.

b- "How to create a Facebook app" quick guide.

c- A sample project.


NOTES: You have to have some basic knowledge of the Facebook API to use it, especially about permissions, the library allows you to interact with Facebook but you are responsible to add/remove the necessary permissions for the actions you want to perform.

You have to create a Facebook app before you can use the library, the instructions are in the quick guide mentioned on item "b" above.


A DEMO app (apk) is also attached for you to take a look at the library's functionality.

Library recompiled on May 28, 2017 (B4A 6.8+)
 

Attachments

  • FacebookDemoApp.apk
    231.9 KB · Views: 776
  • Facebook_Project_And_Guide.zip
    314.1 KB · Views: 1,161
  • Facebook_Lib_1.9.zip
    29.8 KB · Views: 414
Last edited:

Douglas Farias

Expert
Licensed User
Longtime User
B4X:
                TheFields.AddAll(Array As String(fb.USER_CONNECTION_PICTURE, fb.USER_FIELD_NAME, fb.USER_FIELD_GENDER, fb.USER_FIELD_HOMETOWN,"name", "picture.height(200).width(200)"))

this works fine return a image with best quality *-* ty
 

pesquera

Active Member
Licensed User
Longtime User
Hi NDJude, does this library include the facebook chat api? I want to intercept income messages.. thanks
 

RjCl

Member
Licensed User
Longtime User
NJDude,

Its good library, thanks :)

But about the permissions,............I just need the default permissions that Facebook gives when we make app on their site, (profile, email and friends)
How to that in the permissions list?
 

Douglas Farias

Expert
Licensed User
Longtime User
NJ is possible get user photos?
albuns etc?

and how to show this photos in app?
 

NJDude

Expert
Licensed User
Longtime User
You will have to get the Album ID and then get the pictures (URLs), download them and then display them on an ImageView, for more info, refer to the Facebook documentation, using the library you could use "GetFields", something similar to the example I posted before.
 

Douglas Farias

Expert
Licensed User
Longtime User
yes download i know but realy need download all images ? *-*
dont have a way to put this on webview ?
 

RjCl

Member
Licensed User
Longtime User
The default permissions are added automatically, but any extras must be added using the library.

I tried the lib with your demo, but it lets me uploaded, file and message to timeline, even though i didnt give any more permissions and neither does my Facebook app asked for any other than the default ?

Plus, i thought that to publish on behalf of user requires extra permissions for which the Facebook app has to be submitted for approval they are given?
 

Douglas Farias

Expert
Licensed User
Longtime User
NJ sometimes the permission screnn bug show only de x to close, and dont show ok button etc webview click i thing
its possible show this permission only 1 time?
i m using normal login not with permissions
 

Douglas Farias

Expert
Licensed User
Longtime User
hmm but this permission screnn need show all time when user log of and log on again?

i m using this code dont have permissions

B4X:
Sub Button1_Click
fb.Login
End Sub
 

Douglas Farias

Expert
Licensed User
Longtime User
a ok ty man

and is there any chance that one day lib log with the official app of the face?

Only the * question * - *

like this

851580_164407227058622_1947721334_n.png
 

Douglas Farias

Expert
Licensed User
Longtime User
nj excuse me ask you again
I'm really trying to fix this mistake and not understand already tried many, many more ways do not think you can help me?

here is the image with the error attached

my code i m using

B4X:
Sub facebook_Click
fb.Login
End Sub

Sub recebedadosdoface
Private TheFields As List ' declara lista
TheFields.Initialize
TheFields.AddAll(Array As String(fb.USER_CONNECTION_PICTURE, fb.USER_FIELD_NAME, fb.USER_FIELD_LOCATION, "gender", "name", "first_name", "last_name","locale","picture.height(200).width(200)")) 'recebe dados + foto de perfil
fb.GetFields("me", TheFields)          
End Sub


Sub myFB_GetFields_Response(JSONText As String)
    Private jParser2 As JSONParser
    If jParser2.IsInitialized = False Then
    jParser2.Initialize(JSONText)
    End If
    'tenta receber os dados do face em JSON se tiver erro tenta denovo
    Try
    Private Root As Map = jParser2.NextObject
    Catch
    recebedadosdoface
    Return
    End Try
  
    'aqui temos os dados recebidos do face
  
    Private UserPicture As Map = Root.Get("picture") 'mapa da foto''''''''''''''''''''''''''''''''''''''''''''''
    Private Data As Map = UserPicture.Get("data") ' data da foto bytes''''''''''''''''''''''''''''''''''''''''''
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    urlfotoface = Data.Get("url")        'aqui sao os dados declarados no top para serem usados durante todo main
    idfacebook = Root.Get("id")        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    username = Root.Get("name")        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    genderface = Root.Get("gender")    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    primeironome = Root.Get("first_name")
    ultimonome = Root.Get("last_name")
    localefacebook = Root.Get("locale")
  
    'tenta pegar cidade atual
    Try
    Private local As Map = Root.Get("location")
    cidadefacebook = local.Get("name")
    Catch
    cidadefacebook = ""
    End Try
    confereusuarioface
              
End Sub

I tried to see and review the code, but I do not understand sometimes gives error and sometimes not of this error.

You already told me that is why json is not receiving data over there a way to fix this?

already tried looking at your example are more equal do not understand why the error = (
 

Attachments

  • Sem título.png
    Sem título.png
    182.6 KB · Views: 174
Last edited:

NJDude

Expert
Licensed User
Longtime User
A few comments about your code.

If you are using for example fb.USER_FIELD_NAME there's no need to use "...,"name", ..." just use either one.
What's the point to re declare the permissions when an error occurs?, no need to do that.

If the error you're getting "JSON Object expected" happens sporadically, then you will have to take a closer look at the data you're receiving, and move the Try...Catch to see if you can capture the error, debugging is always difficult, that's all I can tell you.
 

RjCl

Member
Licensed User
Longtime User
NJDude,

When having the user logoff from Facebook and then comes back later to login, it makes them re-authorize the Facebook permissions. They seem to be removed when the user logged off ?

Thanks
 
Top