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

stevel05

Expert
Licensed User
Longtime User
Nice one NJDude, I try to stay away from Facebook myself, but I know a lot of people have been asking for an easier way to connect.

Good job:)
 

Douglas Farias

Expert
Licensed User
Longtime User
NJDude i m not in home to test now
this get name country and photo?

i need to get name, years, gender, country and profile photo can i make this with this lib?

can you help me on this?


when i go to home i go test really thx

if it works to get this informations i go send u a donate in paypal man very thx
 

Douglas Farias

Expert
Licensed User
Longtime User
can you post later a simple exemple?

edittext1 = name
edittext2 = country
imageview = profile photo

i m trying to make this many time but not sucess on android :(

here as demo i make in javescript and php if it help you
http://likeapps.net/1.php

here as the javascript and php code
B4X:
<?php

      if(isset($_GET['fblogin'])){
        
              if(isset($_POST)){
                
                
                    IF ($_POST['locale'] == "us")
                    {
                    $a="USA";
                        }
                    else
                    {
                    $a="BRASIL";
                    }

                
                    if($_POST['gender'] == "male") // or if(call())
                    {
                    $b="Homem";
                        }
                    else
                    {
                    $b="Mulher";
                    }
                


                      $msg="<img src='//graph.facebook.com/{$_POST['id']}/picture'> <br>
                      <b>Nome</b> <a href='//fb.com/{$_POST['username']}'>{$_POST['name']}</a>
                      <br><b>Email</b> {$_POST['email']}
                      <br><b>ID</b>  {$_POST['id']}
                      <BR><b>USERNAME</b> {$_POST['username']}
                      <br><b>SEXO </b>  $b
                      <br><b>LOCALIDADE </b> $a
                      ";

                        echo $msg;
  
                }
          
        }
?>
<html>
<head>

<head>





<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
/**    Login Js Sdk by: carafeio      */
window.fbAsyncInit = function(){
Facebook={id:"my key here",url:"www.likeapps.net"};
        FB.init({
                appId      : Facebook.id,
                channelUrl : '//'+Facebook.url+'/channel.html',
                status    : true,
                cookie    : true,
                xfbml      : true
        });
};
(function(d){var js,id='facebook-jssdk',ref=d.getElementsByTagName('script')[0];if(d.getElementById(id)){return;}js=d.createElement('script');js.id=id;js.async=true;js.src="//connect.facebook.net/en_US/all.js";ref.parentNode.insertBefore(js,ref);}(document));
$(function carrega(){
  
        $("img,#casa").click(function(){
          
                FB.login(function(response) {
            
                        FB.api('/me', {fields: 'email,id,username,name,gender,locale'}, function(response) {
                                $("#loadfbLogin").load('1.php?fblogin',response);
                        });
                }, {scope: 'email'});
          
        });
  
});
</script>



</head>
<body>
<div id="fb-root"></div>
        <center>
                <img src="http://vsurvivor.com/images/login_with_facebook.png" width="450">
                <a href="#" id="casa">TESTE </a>
                <div id="loadfbLogin"></div>
        </center>
</body>
</html>
 

Inman

Well-Known Member
Licensed User
Longtime User
Nice.

Should I login every time the user runs the app or just the first time? I think I read somewhere that the access tokens generated using web APIs expire fast compared to those generated using Facebook Android SDK.
 

Inman

Well-Known Member
Licensed User
Longtime User
I haven't found an issue with the access tokens expiring, but, you can call the login anytime if you like.

Great. Is it possible to allow the user to logout and login using a different account?
 

Inman

Well-Known Member
Licensed User
Longtime User
Cool. Thanks for all the help. I was desperately looking for some Facebook integration for one of my apps. Now I can get to work.

BTW, did you check out the Facebook Android SDK? Is it hard to wrap for B4A? I believe the SDK has a logout option.
 

Puck

Member
Licensed User
Longtime User
Dude, you created this in the nick of time, just before I pulled the last hair out my head. Thank you, thank you, thank you.

I now have facebook integrated into my app in less than an hour and I have been trying on my own for more days than I wish to confess. I always ran into a wall.

Your code works perfectly with my code which uses LibGDX and I was having a very tough time getting them to work together. You have saved the day (and my last remaining hair).

If you are using libGDX, you will have to have this run on the main thread instead of being called from libGDX's processes or you get the Java thread error but I figured out how to do that pretty easily (I was having the issue even before your library and had previously figured a way around it).

You are the greatest for helping us out with your libraries. Again, many thanks.
 

Douglas Farias

Expert
Licensed User
Longtime User
one sugestion
NJDude make the same with google+ pls?
get name,photo,country profle and show this in app

thx again good night
 
Last edited:

tuhatinhvn

Active Member
Licensed User
Longtime User
i am new to facebook api
i want to ask about
Dim myWebsite AsString = "<LINK_TO_THE_WEBSITE_IN_REDIRECT_URI>"

Is it important? can i use any url?
 

tuhatinhvn

Active Member
Licensed User
Longtime User
An error occurred with test. Please try again later.

API Error Code: 191
API Error Description: The specified URL is not owned by the application
Error Message: Invalid redirect_uri: Given URL is not allowed by the Application configuration.
 

Douglas Farias

Expert
Licensed User
Longtime User
same with login

{
"error": {
"message": "redirect_uri isn't an absoluted URI. Check RFC 3986.",
"type": "OAuthException",
"code": 191
}
}



when i try another button show me

Facebook Error
Cannot post message to the timeline

an active acess token must be used to query information about the current user.

Close


here is the image
 

Attachments

  • Untitled-1.jpg
    Untitled-1.jpg
    310.7 KB · Views: 251
Top