Android Example Using Facebook Login Without Social SDK

First up, there is a fantastic library called 'Social API SDK Wrappers' written by a guru 'Periklis Koutsogiannis'. If you need Facebook or any other social media related integration, please refer to that library. Chances are that your use case will be covered by it in a much cleaner way.

Additionally, before proceeding with this tutorial, please make sure to read up on OAuth, Twitter SDK, Facebook API using their official docs. That will help establish the context required. Sorry, but I don't want to duplicate the effort and potentially provide incomplete information about those APIs.

Now, this tutorial is for the use case when the Social API doesn't cater to your requirements. In my project, I had a requirement to send a Facebook/Twitter post in the background from a service. In this case, the Social API cannot be used as it requires an Activity.

After you have got your API key/secret from Facebook, following is an overview of the steps. I have also attached a sample project to this tutorial for posting a status update to Facebook.

  • Since, Facebook uses OAuth for login, there are two steps to logging in to Facebook.
    • Get an auth token
    • Use the auth token and get an access token to use with actual Facebook API calls(such as status updates). Note that you should save the access token in your application when the user has given his/her consent to your application. Also, as per Facebook's documentation, the token will expire in around 60 days, so your application should check for this and prompt the user as such.
  • In the first step, we get the auth token from Facebook using the browser. This is because if an access token hasn't already been granted to our application, we want to use Facebook's own 'login dialog'. The method I have chosen to go with is the standard web login dialog. You can also look into using their SDK but that was more of a pain (using reflection and custom editing of manifest etc) than it is worth.
  • The web login will show the standard Facebook login page and ask the user to sign in. After signing in, the user will be shown your application's name and logo and a list of permissions will be presented to the user to authorize.
  • If the user decides to grant access to your Facebook application, they will be redirected to the URL that you specified in your Facebook developer console with the auth token appended to the URL.
  • We use this redirect URL in our WebView_PageFinished event to extract the auth token. At this point, the first step in the login process is complete.
  • Now we use this auth token to call Facebook's access token service to generate an access token for our application. This uses your FB app id, FB app secret, FB redirect URL(note that this should be exactly the same that you specified earlier) and the auth token generated above. For this, I have used the HttpUtils class. This class needs a method to callback when the access token service returns a response. In the attached example, you can see that I have initialized the HttpUtils to call the 'OnAPICallComplete' in 'Main' Activity. The HttpUtils uses the HTTP GET method to request the access token, in case you are interested!
  • When the callback method is called upon by HttpUtils, the method request the FacebookConnector class to store it. Now we are ready to use this token to do things on behalf of the user!
  • The button click event handler calls the Facebook helper class to post to Facebook. That call uses HttpUtils to do an HTTP POST, passing our access token and the message to Facebook's API.

A few notes:
The FacebookConnector helper class does not do a lot of error handling. It is after all a piece of work that is meant to be a draft. Please handle errors appropriately. The class does have a method that allows you to check if the auth flow has been completed or not(access token received or not).

Hope this will be of a bit of help to some.

HH.
 

Attachments

  • SocialExample.zip
    12.6 KB · Views: 817

cambopad

Active Member
Licensed User
Longtime User
Thanks you for sharing this tips! I am also looking for it as I am experiencing many problems using FB sdk. Anyway, will the Access Token generated this way live shorter than the one generated using the FB mobile SDK?
 

Haris Hafeez

Active Member
Licensed User
Longtime User
Thanks you for sharing this tips! I am also looking for it as I am experiencing many problems using FB sdk. Anyway, will the Access Token generated this way live shorter than the one generated using the FB mobile SDK?

Facebook's official documentation states that an Access Token generated using their web services will be a short lived one(one day if I recall it correctly) while the tokens generated using their Android SDK will have a longer life of around 60 days.

However, in my experience, the Access Token that is generated by the web service also has the longer ~60 days of expiry.

You may want to debug the FacebookConnector class and check the expiry value that is returned to get an idea of how long the token is valid for.
 

Noize

Member
Licensed User
Longtime User
Hi,

I'm trying to use ur sample wich is very interesting , but, I have an error on "Private FB_CRED_STORE As KeyValueStore" because I don't have the proper library to run it, wich library is?

Thanks
 

Reids

Member
Licensed User
Longtime User
Hi I'm missing "Keystore" library too,I search forum and cannot find it thanks
 

Attachments

  • keystore.PNG
    keystore.PNG
    5.1 KB · Views: 457

Reids

Member
Licensed User
Longtime User
@Erel Thanks erel I just noticed that keystore wasn't a library it a class and must be added via module, I search several times cannot find any jar and xml library cause it a module :)

@Noize just copy your KeyValues.bas from link http://www.b4x.com/android/forum/attachments/keyvaluestore-zip.20066/
and goto Project > Add existing module > select "KeyValues.bas" and you're ready to go

@Hariz get more error while compiling :(
 

Attachments

  • error_signer.PNG
    error_signer.PNG
    19.9 KB · Views: 397
Top