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: 710
  • Facebook_Project_And_Guide.zip
    314.1 KB · Views: 1,075
  • Facebook_Lib_1.9.zip
    29.8 KB · Views: 349
Last edited:

walterf25

Expert
Licensed User
Longtime User
I don't know what the problem might be, but it has to be with your settings for example, the only thing I see different with my test app is that the field: SOCIAL DISCOVERY is set to YES.
NJ i can confirm that issue, while trying to get the library to work again, i got this message popped up, i've gone through the settings on the facebook developer console over and over and so far everything seems to be fine, but i still can't connect, and now i get this error
https://www.dropbox.com/s/41ey50ex7cog6hx/Screenshot_2014-06-29-22-51-31.png
 

dibesw

Active Member
Licensed User
Longtime User
HI for all,
is it possible to centre fb screen?
I have samsung tab3. THANKS.

centra.jpg
 

NJDude

Expert
Licensed User
Longtime User
That's strange, the FB screen should cover the whole screen, there's nothing to center.

I will check the lib anyway. Thanks for the report.
 
Last edited:

dibesw

Active Member
Licensed User
Longtime User
Thanks NYDude, I solve it; problems is ScaleAll.

Another question:
Get Profile must be run with a button.
This could be a problem
Is there no way for execute automatically after login?
tell me about it
THANKS!
 

NJDude

Expert
Licensed User
Longtime User
Thanks NYDude, I solve it; problems is ScaleAll.

Another question:
Get Profile must be run with a button.
This could be a problem
Is there no way for execute automatically after login?
tell me about it
THANKS!
You can do it using the Login_Response event, for example:
B4X:
Sub FB_Login_Response(Success As Boolean)

    fb.GetMyProfile	
				
End Sub
That event will trigger after a successful login.
 

Mashiane

Expert
Licensed User
Longtime User
Wow, this is super brilliant! I have installed the apk on my device and it works like a charm. On running the facebook_project_and_guide b4a project however my device seems to login fine but then goes directly to the specified URL and stays there and does not go back to the main screen. Can you please advise what I could have done wrongly or perhaps the source code has been updated?

Thanks.
 

Mashiane

Expert
Licensed User
Longtime User
Can you please provide a newer guide example project please. I want a user to be able to login and if the login is successful get the user details like mobile number and full name then display these in a text box. I understand that there is a Login_Response event that is mentioned somewhere but dont know how that event is parsed to be able to continue.

Again thanks for such a brillian piece of a library.
 

NJDude

Expert
Licensed User
Longtime User
You have to use GetProfile, that will return the profile of the selected user:
B4X:
fb.GetProfile("john.doe")
You can add that to the Login_Response event and then extract the fields you need.
 

Mashiane

Expert
Licensed User
Longtime User
You have to use GetProfile, that will return the profile of the selected user:
B4X:
fb.GetProfile("john.doe")
You can add that to the Login_Response event and then extract the fields you need.

Thanks, managed to get it working. Just another question, the .GetProfile method, is it only limited to particular fields? If I want to get the mobile number and profile picture of an individual, how do I do so?
 

NJDude

Expert
Licensed User
Longtime User
To get other fields you have to add the corresponding permissions, note that some users might not have those fields filled (phone, location, etc) so you have to anticipate that and write your error control properly.
 

TheWind777

Active Member
Licensed User
Longtime User
I have a SHARE button and a LIKE button.

I'm sure that this library does both... but I see no reference to either SHARE or LIKE in any of these 9 pages on the forum.

When a website has a SHARE button... what is it doing? Is it just posting a picture and associated text on the guy's wall?

And, if that is all they are doing, then would that just be a FACEBOOK LOGIN, POST TO TIMELINE of the text and UPLOAD TO TIMELINE of the icon?

How would one post to the timeline and have the image of the icon be in the same post. If I post first and then upload the picture would that be added as two things on the guy's timeline?

...

Also, when I post to my Facebook page... it gives me:

(#200) Permissions Error

And the error is FORBIDDEN.

I am sure that is some option I have opted-out of in Facebook. How do I get Facebook to accept such posts to my wall (because I'll have to explain to them what they have to do to turn it on so the SHARE will succeed.


...

However, I don't have a clue what LIKE does. Can your library be used to do a LIKE, and is there an example somewhere? I suppose that some counter needs to be triggered and it is probably some automatic function that's available through the FaceBook App choices, maybe? Would I have to ask the FaceBook App guys for special permission to allow a LIKE, and then how would I use this library to do it? (Or is that even possible).
 

boten

Active Member
Licensed User
Longtime User
NJDude,
I need to post a message to facebook timeline.
Using your lib, relevant code is (after user is logged on):

B4X:
Sub msg_Click
  ProgressDialogShow("Posting to Facebook...")
  fb.PostToTimeline("me", "some message","")
End Sub

Sub myFB_PostToTimeline_Response(MessageID As String)
  ProgressDialogHide
  Msgbox("Message Poseted","OK")
End Sub

Sub myFB_Facebook_ErrorMessages(Message As String)
    Msgbox2(Message, "Facebook Error", "Close", "", "", Null)
End Sub

I got the message posted on timeline, but sometimes the ProgressDialog keeps on spinning and after a while I get
2 msgboxes as follows:


Processing Error
java.lang.NullPointerException
(close)


after pressing (close), a 2nd msgbox appears:

Connection Error
java.net.SocketTimeoutException: Read times out
(close)

pressing (close) on this one just returns to the app, as it should be.

Is there a way to catch this "timeout" AND still get the _PostToTimeline_Response event?
 

TheWind777

Active Member
Licensed User
Longtime User
NJDude,
I need to post a message to facebook timeline.
Using your lib, relevant code is (after user is logged on):

B4X:
Sub msg_Click
  ProgressDialogShow("Posting to Facebook...")
  fb.PostToTimeline("me", "some message","")
End Sub

Sub myFB_PostToTimeline_Response(MessageID As String)
  ProgressDialogHide
  Msgbox("Message Poseted","OK")
End Sub

Sub myFB_Facebook_ErrorMessages(Message As String)
    Msgbox2(Message, "Facebook Error", "Close", "", "", Null)
End Sub

I got the message posted on timeline, but sometimes the ProgressDialog keeps on spinning and after a while I get
2 msgboxes as follows:

Processing Error
java.lang.NullPointerException
(close)


after pressing (close), a 2nd msgbox appears:

Connection Error
java.net.SocketTimeoutException: Read times out
(close)

pressing (close) on this one just returns to the app, as it should be.

Is there a way to catch this "timeout" AND still get the _PostToTimeline_Response event?


So, your declaration of fb is in the Globals section, not the Process_Globals section or activity_create section; and the initialization of fb is in the Activity_Create section... but not in the, "if firsttime then/end if section?"

And, the dim AppID as String = "12552124124551234", AppSecret and myWebsite declarations are in the globals section?

...

Did you try:

Try
Msgbox2(Message, "Facebook Error", "Close", "", "", Null)
Catch
' Whoops
End Try

Also, shouldn't there be a ProcessDialogHide in the
myFB_Facebook_ErrorMessages() routine, as well?
 

TheWind777

Active Member
Licensed User
Longtime User
To get other fields you have to add the corresponding permissions, note that some users might not have those fields filled (phone, location, etc) so you have to anticipate that and write your error control properly.

I am having three problems.

First is probably a very simple one. When using any example (such as the Facebook Guide example), when I try to POST to the wall it always gives an error #200, then says:
"The User hasn't authorized the application to perform this action."

When I try to do an Upload, it says:
(#200) Permission Error)
FORBIDDEN

And that is when I use your Example. Also does the same when I put it into my program. I have figured it must be some setting I have set in Facebook account (like don't post to my wall, or something) but it doesn't seem to be restricted.

...

Second problem is... I want to be able to have an active LIKE button with the number of likes displayed and a SHARE button.

Facebook has said, "Initialize the JavaScript SDK using this app: ACIMWow"

That is my Facebook App Name.

It also says to use this Javascript code:

FB.init({
appId : '{your-app-id}',
status : true,
xfbml : true,
version : 'v2.0'
});


<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&appId=387721248047129&version=v2.0";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>

<div class="fb-like" data-href="http://acimwow.angelfire.com/" data-layout="standard" data-action="like" data-show-faces="true" data-share="true"></div>


Can I implement a LIKE and SHARE button using your library?

Do you have any example that might have a working LIKE and SHARE button?

( First I have to be able to do the simplest thing... which is post a message to the person's wall, though).
 

boten

Active Member
Licensed User
Longtime User
.....
Also, shouldn't there be a ProcessDialogHide in the
myFB_Facebook_ErrorMessages() routine, as well?

fb declared in globals.
fb initialized in activity_create ALWAYS.
put ProcessDialogHide in myFB_Facebook_ErrorMessages()
added try/catch

still, sometimes I get the above error msgboxes:
(a) the text IS posted on timeline
(b) ProgressDialogShow("Posting to Facebook...") is still spinning
(c) got the 2 above error msgboxes
(d) myFB_PostToTimeline_Response(MessageID AsString) is never invoked - put a log(...) there, not appearing on log
 

boten

Active Member
Licensed User
Longtime User
Need your help in facebook app (posting to timeline)

The attached zip is my simple project

The user can enter a message in the edittext and click the Post button.

If user is logged in - will post msg
if NOT logged in - will login with permissions and then post msg

2 problems:
problem #1 the most crirical:
Now, if I log in with my own fb user - the fb user that created the fb app - msg is posted on my timeline

If a differnet user run and logs in with his user - msg is NOT posted and

myfb_Facebook_ErrorMessages(Message As String) return the message:

(#200) The user hasn't authorized the application to perform this action


problem #2 just annoying
When I login with permission to my own fb account, fb still shows 2 screens:
a) asking the user to verify getting his profile
b) asking the user to verify posting on his timeline

What am I doing wrong?
 

Attachments

  • fbpost.zip
    6.9 KB · Views: 139
Top