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

ivanomonti

Expert
Licensed User
Longtime User

Attachments

  • a01.jpg
    a01.jpg
    21.3 KB · Views: 214
  • a02.jpg
    a02.jpg
    38.4 KB · Views: 212

analizer3816

Member
Licensed User
Longtime User
***I use this code and it's work***
Sub LoginButton_Click

fb.LoginWithPermissions(Array As String(fb.PERMISSION_PUBLISH_ACTIONS))

End Sub
 

ivanomonti

Expert
Licensed User
Longtime User
yes perfect,

what are the permits, there is a full list of permissions that can be added, such as calendars, message etc. etc.
 

ivanomonti

Expert
Licensed User
Longtime User
***I use this code and it's work***
Sub LoginButton_Click

fb.LoginWithPermissions(Array As String(fb.PERMISSION_PUBLISH_ACTIONS))

End Sub

continuously have errors in my project, give me help you understand where I'm wrong!

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'facebook module
    Dim fb As Facebook
    Dim AppID As String = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
    Dim AppSecret As String = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    Dim myWebsite As String = "http://www.ivanomonti.eu"
End Sub

Sub Activity_Create(FirstTime As Boolean)
    fb.Initialize(Activity, Me, "myFB", AppID, AppSecret, myWebsite)
    LoginFacebook
    Dim bt As Button
    bt.Initialize("bt")
    bt.Text = "post"
    Activity.AddView(bt,10dip,10dip,100%x-20dip,42dip)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub bt_click
    PostToTimelineFacebook("Test message",myWebsite)
End Sub


#region facebook

Sub LoginFacebook
    fb.LoginWithPermissions(Array As String(fb.PERMISSION_PUBLISH_ACTIONS,fb.PERMISSION_PUBLISH_STREAM))
End Sub

Sub PostToTimelineFacebook(message As String, link As String)
    fb.PostToTimeline("me", message, link)
End Sub

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

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

    ToastMessageShow(Temp,True)              
  
End Sub

#End Region

Error: Bad Request
 

ivanomonti

Expert
Licensed User
Longtime User
[QUOTE = "analizer3816, posta: 381050, membro: 25325"] è necessario compilare il tuo sito web in linguetta avanzata (vedere in a01.jpg) -> OAuth valido redirect URI [/ QUOTE]

non si dispone di un esempio, io mi sto perdendo
 

ivanomonti

Expert
Licensed User
Longtime User
link redirectory = http://www.ivanomonti.eu/apifacebook/facebook.html

code page html


B4X:
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
<title>cart</title>
</head>
<body>
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'xxxxxxxxxxxxxxxxxxxxx',
      xfbml      : true,
      version    : 'v2.5'
    });
  };

  (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";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));
</script>
</body>
</html>

that way I do not have error json, but I can not post the arrivandomi bad request

B4X:
    Dim fb As Facebook
    Dim AppID As String = "xxxxxxxxxx"
    Dim AppSecret As String = "xxxxxxxxxxxxxxxxxxxxxxx"
    Dim myWebsite As String = "http://www.ivanomonti.eu/apifacebook/facebook.html"

HELP :-(
 

ivanomonti

Expert
Licensed User
Longtime User
I create html file with your code on my server, it never error json
when I use http://www.ivanomonti.eu/apifacebook/facebook.html, it error json



I regenerated new app of facebook, leaving all settings as default, put the link redirectory, but the problem persite.

I point if the file does not have a problem with the message json, but then I can not prune, if you do not point the full link redirectory me by mistake but I can post json.

I do not know how to move in this situation
 

ivanomonti

Expert
Licensed User
Longtime User
[QUOTE = "analizer3816, posta: 381.501, membro: 25325"] è possibile verificare con il mio link http://www.phothitham.ac.th/facebook.html e vedere il mio codice per confrontare con il tuo

il mio server uso iis8.5 su win2012r2 [/ QUOTE]

HTML:
<html>
    <head>
        <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
        <title>facebook</title>
    </head>
    <body>
        <script>
          window.fbAsyncInit = function() {
            FB.init({
              appId      : '1667411463500049',
              xfbml      : true,
              version    : 'v2.5'
            });
          };
          (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";
             fjs.parentNode.insertBefore(js, fjs);
           }(document, 'script', 'facebook-jssdk'));
        </script>
        <div class = "fb-like"-share dati = "true"-width dati = "450" dati-show-facce = "true"> </ div>
    </body>
</html>

thank
 

ivanomonti

Expert
Licensed User
Longtime User
I have not managed to solve the problem json to login, I can post it on facebook, but the error message of json and ugly to look at, I want to solve it, who gives me a hand with.
 

Leni Berry

Active Member
Licensed User
Longtime User
Hi,

how to get email address after login successfully?
i 'hv added permission fb.USER_FIELD_EMAIL and added also to fb.getfields array. but, the email not shown in JSONText under Sub myFB_GetFields_Response(JSONText As String).
i can get all field like name, picture, id and gender. but not for the email
Please help me...
 

Leni Berry

Active Member
Licensed User
Longtime User
what a mistake...
permission added is wrong permission...
i hv added permission fb.permission_email and the email show in JSONText object...
 
Top