Fork us on GitHub

Using The New Facebook API

We recently introduced a new Facebook API to allow native int...
Post Image

Using The New Facebook API

Picture
We recently introduced a new Facebook API to allow native integration since Facebook has stated their intention to no longer accept OAuth logins from applications. This is very much work in progress so some things will change as we move along especially related to elevated permissions for posting.

If you just want something in the form of a "share button" we suggest you refer to the builtin share button which uses native sharing on both iOS and Android.

The new Facebook API is very simple, in fact as of this writing it includes only 5 methods. Notice that the old Facebook API will still work as expected with the exception of the login aspect which will be handled by the new API.
There are really 3 significant method s in the API, login/logout and isLoggedIn(). You can also bind a listener to login event callbacks which is really pretty simple. The difficulty isn't here though.

Before you get started you need to go to the page on Facebook for app creation: https://developers.facebook.com/apps
Here you should create your app and make sure to enter the package name of the Codename One application both for the section marked as Bundle Id and Package Name (see the red highlighting in the attached image).

Once you do that you need to define the build argument facebook.appId to the app ID in the Facebook application (see the red marking at the top of the image).

Now when you send a build and invoke FacebookConnect.login() this should work as expected on iOS but it will fail on Android. The reason is that Facebook requires a hash from Android developers to identify your app. However, their instructions to generate said hash don't work... The only way we could find for generating the hash properly is on an Android device.
If you have DDMS you can connect the device to your machine and see the printouts including the hashcode (notice the hashcode will change whenever you send a debug build so make sure to only use Android release builds). You can also get the value of the hashcode from Display.getInstance().getProperty(" facebook_hash", null);
This will return the hash only on Android ofcourse.

You can take this hash and paste it into the section marked Key Hashes in the native android app section. Notice you can have multiple hashes if you have more than one certificates or applications.

Once login is successful the existing facebook API's from the Facebook package should work pretty much as you would expect.

Notice: This post was automatically converted using a script from an older blogging system. Some elements might not have come out as intended.... If that is the case please let us know via the comments section below.

Share this Post:

Posted by Shai Almog

Shai is the co-founder of Codename One. He's been a professional programmer for over 25 years. During that time he has worked with dozens of companies including Sun Microsystems.
For more follow Shai on Twitter & github.