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:

leitor79

Active Member
Licensed User
Longtime User
Hi,

Nice work, thank you!

Just to be sure before downloading things: with this library, can I get the friends contact list of the logged user? SOAP or XML? (or which format?)

Thank you!
 

leitor79

Active Member
Licensed User
Longtime User
Hi NJDude, thanks fpr your answer.

How can I get my friend's list? I've tried several permissions and fields, but I can only get "me" at best (my ID and my name); I've triend using this:

B4X:
ThePermissions.Add(FB.PERMISSION_READ_FRIENDLISTS)
                ThePermissions.Add(FB.PERMISSION_USER_FRIENDS )
                ThePermissions.Add(FB.PERMISSION_MANAGE_FRIENDLISTS)

Or it's maybe a field issue? I'm using:

B4X:
TheFields.AddAll(Array As String(FB.USER_FIELD_ID, FB.USER_FIELD_NAME, FB.USER_CONNECTION_FRIENDS,FB.USER_CONNECTION_FRIENDLIST) )

Also; there appears to be an encoding issue: the JSONText of the Getfields_Response events returns "\u00ed" instead of "í". Is there a way to specify the encoding?

Thank you very much! Great library!
 

RjCl

Member
Licensed User
Longtime User
Hi NJDude, thanks fpr your answer.

How can I get my friend's list? I've tried several permissions and fields, but I can only get "me" at best (my ID and my name); I've triend using this:

B4X:
ThePermissions.Add(FB.PERMISSION_READ_FRIENDLISTS)
                ThePermissions.Add(FB.PERMISSION_USER_FRIENDS )
                ThePermissions.Add(FB.PERMISSION_MANAGE_FRIENDLISTS)

Or it's maybe a field issue? I'm using:

B4X:
TheFields.AddAll(Array As String(FB.USER_FIELD_ID, FB.USER_FIELD_NAME, FB.USER_CONNECTION_FRIENDS,FB.USER_CONNECTION_FRIENDLIST) )

Also; there appears to be an encoding issue: the JSONText of the Getfields_Response events returns "\u00ed" instead of "í". Is there a way to specify the encoding?

Thank you very much! Great library!

I think its only the friends who have actually used your Facebook app that will show
 

leitor79

Active Member
Licensed User
Longtime User
I think its only the friends who have actually used your Facebook app that will show

Hi RjCl! Thanks for your answer!
Yes, the USER_FRIENDS I've read it's about the friends who uses the app. But I need a list of all the friends... or how can I select friends I would like to send a message or invite to use the app?

Add; I also manage to obtain some kind of friend's grups (like "Acquaintances" or "Family") but not the friends on them?

Thank you!
 

Douglas Farias

Expert
Licensed User
Longtime User
HI @NJDude its possible remove the ProgressDialogHide from the results?
for exemple
i dont want ProgressDialogHide when i acess this Sub myFB_GetFields_Response(JSONText As String)
i think you added ProgressDialogHide this on your lib *-*
can remove this pls?

and can give to user chance to change the connection error text ? i m portugues i want to translate this but it is on your lib *-*
 

NJDude

Expert
Licensed User
Longtime User
Hi RjCl! Thanks for your answer!
Yes, the USER_FRIENDS I've read it's about the friends who uses the app. But I need a list of all the friends... or how can I select friends I would like to send a message or invite to use the app?

Add; I also manage to obtain some kind of friend's grups (like "Acquaintances" or "Family") but not the friends on them?

Thank you!
I'm not really sure but it seems Facebook has changed some permissions, the APIs restricts certain actions to prevent abuse and spamming, maybe that's the case.
 

NJDude

Expert
Licensed User
Longtime User
HI @NJDude its possible remove the ProgressDialogHide from the results?
for exemple
i dont want ProgressDialogHide when i acess this Sub myFB_GetFields_Response(JSONText As String)
i think you added ProgressDialogHide this on your lib *-*
can remove this pls?

and can give to user chance to change the connection error text ? i m portugues i want to translate this but it is on your lib *-*
Removing the ProgressDialogHide can be done, no problem, but the error messages cannot be translated since they come directly from Facebook.
 

leitor79

Active Member
Licensed User
Longtime User
I'm not really sure but it seems Facebook has changed some permissions, the APIs restricts certain actions to prevent abuse and spamming, maybe that's the case.

Thanks NJ!

And what about the encoding issue?

Also; could you post some information about the CustomRequest method? I'm thinking about doing some tests with the friend's list (like using this thing I couldn't find on your library) but I couldn't make it work (nothing happens):

B4X:
FB.CustomRequest("GET","/<a list id>/members","myFB")

then

B4X:
Sub myFB_CustomRequest_Response(JSONText As String)

But this sub is never triggered...

Thank you!
 

NJDude

Expert
Licensed User
Longtime User
@leitor79

To use CustomRequest you need a request ID, your call is almost correct, for example, say you want to get your posts, then you have to do this:
B4X:
fb.CustomRequest("get", "me/feed?", "Custom") 'Custom is the ID of this call, in case you have several.

The result will be shown on the corresponding event:
B4X:
Sub FB_CustomRequest_Response(JSONText As String, ID As String)
Also, as you can see on the top on the screen on THIS link, Facebook has removed that feature, the new API (2.0) doesn't seem to have that either.

Facebook is always changing the APIs, things that work today won't work tomorrow, also, there are many things that APIs cannot do, like I said previously, to prevent spamming, abuse and protect privacy.
 

RjCl

Member
Licensed User
Longtime User
@leitor79

To use CustomRequest you need a request ID, your call is almost correct, for example, say you want to get your posts, then you have to do this:
B4X:
fb.CustomRequest("get", "me/feed?", "Custom") 'Custom is the ID of this call, in case you have several.

The result will be shown on the corresponding event:
B4X:
Sub FB_CustomRequest_Response(JSONText As String, ID As String)
Also, as you can see on the top on the screen on THIS link, Facebook has removed that feature, the new API (2.0) doesn't seem to have that either.

Facebook is always changing the APIs, things that work today won't work tomorrow, also, there are many things that APIs cannot do, like I said previously, to prevent spamming, abuse and protect privacy.

Could this be used for inviting friends ? https://developers.facebook.com/docs/games/invitable-friends/v2.0
 

Douglas Farias

Expert
Licensed User
Longtime User
Removing the ProgressDialogHide can be done, no problem, but the error messages cannot be translated since they come directly from Facebook.
Please let me know when this version available? *-*

and new sugestions

its possible add event

fb.isWebviewvisible = true or false
fb.Xbutton = true or false
fb.webviewvisible = true or false

i want to add when back key pressed
if fb.isWebviewvisible = true then
fb.webviewvisible = false
return
end if

*-*

and X button to close webview optional or user can close using back key or X button

only is a sugestions *-*
 

leitor79

Active Member
Licensed User
Longtime User
Hi NJ, thank you for your answer; I've could manage to do the custom request.

Now I'm having trouble with the PostToTimeline function. I assume it could be used to post on my own wall? I use this way:

FB.PostToTimeline(u,"this is a test",v)

being:
u - my user ID, obtained by the GetFields method
v - a random link.

Also; I don't know how to use (if I can use) the CustomRequest function in order to test a function if I have to send a parameter (to manually post a message using this, look for the "publishing" paragraph)... can it be done?

Thank you very much!
 

NJDude

Expert
Licensed User
Longtime User
Hi NJ, thank you for your answer; I've could manage to do the custom request.

Now I'm having trouble with the PostToTimeline function. I assume it could be used to post on my own wall? I use this way:

FB.PostToTimeline(u,"this is a test",v)

being:
u - my user ID, obtained by the GetFields method
v - a random link.

Also; I don't know how to use (if I can use) the CustomRequest function in order to test a function if I have to send a parameter (to manually post a message using this, look for the "publishing" paragraph)... can it be done?

Thank you very much!
Yes, if you use CustomRequest just type the line as it is, for example:
B4X:
fb.CustomRequest("get", "/me/feed?with=location", "Custom")
One way to test and get the right queries is by using the Graph API Explorer, you can just copy the query and create your custom request using the library.
 

NJDude

Expert
Licensed User
Longtime User
Now I'm having trouble with the PostToTimeline function. I assume it could be used to post on my own wall? I use this way:

FB.PostToTimeline(u,"this is a test",v)

being:
u - my user ID, obtained by the GetFields method
v - a random link.
You just need to do this:
B4X:
FB.PostToTimeline("me","this is a test",v)
 
Top