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
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.

Hi, NJ!

You mean copying the text I've tried after "v2.0/" (ex.: "me?fields=id,name") into the "Request" parameter of the customrequest function?

sshot-731.png

Thank you very much!
 

leitor79

Active Member
Licensed User
Longtime User
Hi,

I can't manage to make invitations using this library. The invites are for games (my app is not) and using the customrequest I can't use this and the PostToTimeline doesn't work (I'm trying to post on my own wall):

FB.PostToTimeline( "me","this is a test", "http://www.google.com")

That does nothing. Just black screen. I'm using this permissions (more than I need, I know):

ThePermissions.Add(FB.PERMISSION_READ_FRIENDLISTS)
ThePermissions.Add(FB.PERMISSION_USER_FRIENDS )
ThePermissions.Add(FB.USER_CONNECTION_FRIENDLIST)
ThePermissions.Add(FB.PERMISSION_FRIENDS_ONLINE_PRESENCE)
ThePermissions.Add(FB.PERMISSION_PUBLISH_ACTIONS)
ThePermissions.Add(FB.PERMISSION_PUBLISH_STREAM)

Any ideas?

Thank you!
 

NJDude

Expert
Licensed User
Longtime User
You cannot send invitations via the API, it says it on the page:
Facebook messages are a channel for person-to-person communication, and not for apps to send messages, or encourage people to spam their friends. In general, games on Facebook.com should use requests when communicating in game status (its your turn), inviting people to use an application, or sending messages to multiple people. You should offer the Send Dialog in situations when someone might otherwise send an email.

This line works:
B4X:
FB.PostToTimeline( "me","this is a test", "http://www.google.com")
After posting a message ID will be returned in the following event:
B4X:
PostToTimeline_Response(MessageID As String)
Have you added that?
 

leitor79

Active Member
Licensed User
Longtime User
You cannot send invitations via the API, it says it on the page:


This line works:
B4X:
FB.PostToTimeline( "me","this is a test", "http://www.google.com")
After posting a message ID will be returned in the following event:
B4X:
PostToTimeline_Response(MessageID As String)
Have you added that?

Hi NJ,

Thanks for your answer. I did not tried capturing the response, I've done it now, but nothing happens. But I've seen the examples again and I've noticed the ErrorMessages event, I've captured that and I get an error:

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

I'm not sure if this has something to do with your library. I've read many many pages, changed a lot of app settings, I've only get to change the error message (like "API calls from the server require an appsecret_proof argument") but I could never managed to post on my timeline. It's over a week I'm on this problem, "downgrading" my solution (from getting a list of users to invite, to post on my wall, passing by sending a message to one user at a time) because nothing of this works (not your fault), and before giving up and start over this project on eclipse and android api I'm asking if maybe other facebook user has encountered this problem and how could he/she solved it.

Thank you very much!
 

leitor79

Active Member
Licensed User
Longtime User
Hi,

I've made the PostToTimeline to work.

The thing I was doing wrong was to put all the code on the same event. For testing purposes, I had from the login to the PostToTimeLine code all together. I suppose that there is required to do some process after authentication that takes some time and my app did not wait for it.

Thanks NJ for your help anyway!
 

asales

Expert
Licensed User
Longtime User
Hello NJDude.

After login the app shows the first page of my site configured in "Valid OAuth redirect URIs" of Facebook and in the variable "Private myWebsite As String" of the app.
It is possible to disable it and after login returns to the main form of app?
 

NJDude

Expert
Licensed User
Longtime User
I'm not sure I understand what you want, but you cannot alter the login sequence, however, you could query if the user is logged in or not using "IsUserLoggedIn" and do whatever you need.
 
Last edited:

asales

Expert
Licensed User
Longtime User
Hi NJDude, Thanks.
The login sequence is: (1) insert user and password, (2) click in the login button, (3) show Facebook permissions to authorize, (4) the app show the homepage of my site and (5) returns to the main screen of app.
I want to disable the item 4: "show the homepage of my site".
It's possible?
 

leitor79

Active Member
Licensed User
Longtime User
The issue AS2 describes happens to me too. After the user press the login button on the facebook login window, the site configured on the site url is shown for a second or two....

Regards,
 

NJDude

Expert
Licensed User
Longtime User
Yes, that's supposed to happen, that's how the API works, however, you could point the URL to a blank html document, something like:
B4X:
http://www.yourwebsite.com/dummy.html
It will still show for a second but you will see just a blank screen.
 

brelto85

Active Member
Licensed User
Longtime User
after called the LogIn method, returns the following error:

"Please verify you have internet Access"

The data connection is active
Where is the problem?
 

brelto85

Active Member
Licensed User
Longtime User
yes, AppID AppSecret e WebSite are correctly setted
the login page and the facebook permissions page are shown correctly and the login flow works correctly
After that, the IsUserLoggedIn property are setted correctly but after show the message error
 

brelto85

Active Member
Licensed User
Longtime User
I found a problem that Facebook library can not know if it is something to fix or define that beta or sale. When I change the name of the packege bookstore example show always ends with error Connection Error. Please Verify You have Internet Access. However the authentication process works despite the message but it is not a good presentation. Is only run if the packege name is njdude.facebook.guide

I could work with this issue.

also the edwar8405 have the same error
 

NJDude

Expert
Licensed User
Longtime User
Well, on the post you indicate, he's done something, no idea what, therefore my answer on the next post, in your case, I don't know what is the problem, unless you've done something you're not telling me.
 
Top