Sunday, September 18, 2011

LWUIT and Facebook API - by Chen Fishbein

Nowadays almost every app needs to be connected to facebook in some level: post a message to your friends wall, post on your wall, like a post, etc...

We are well aware of such need and we are well aware of the lack of support for J2ME devices.
Connect an app to facebook sounds something trivial that every smart phone is doing very easily, but doing the same on a J2ME device is almost impossible.

Facebook uses Oauth2 to authenticate which basically means you need a browser to display the login page and the ability to handle a session on the client until login is authenticated.

In the last couple of months we worked very hard to finally offer such solution to LWUIT developers, now you can use a very simple API to access Facebook.(it's important to note that this won't work on all J2ME devices, due to a relatively new/complex certificate facebook is using, which some old devices has difficulties with, even the good old WTK won't work - this will require a nokia SDK or the upcoming SDK 3.0.5 from Oracle which should be released soon)

The new API has a new Demo which is available from our svn:
https://svn.java.net/svn/lwuit~svn/trunk/Apps/FaceBookDemo

In addition we created an official facebook lwuit page which is open for lwuit discussions


Some API samples:

//authenticate your facebook app with the permissions you need
FaceBookAccess.getInstance().authenticate(appid, redirect_uri, new String[]{"user_photos", "friends_photos", "publish_stream", "read_stream", "user_relationships", "user_birthday", "friends_birthday", "friends_relationships", "read_mailbox", "user_events", "friends_events", "user_about_me"});

//then create a User Object and ask the FaceBookAccess to fill it with the data
User me = new User();
FaceBookAccess.getInstance().getUser("me", me, null);

19 comments:

  1. awesome awesome awesome!!! thanks so much!

    ReplyDelete
  2. Does it support on Nokia S60 devices?

    ReplyDelete
  3. I could not find the Java ME SDK 3.0.5 right now, which version of Nokia SDK can be used?

    ReplyDelete
  4. Hi, Shai, the link you provided is for SDK 3, which is not working on the demo.

    ReplyDelete
  5. I used the LWUITtheme.res to replace the res file in the demo app, this time it is much better, but I found in the permission request page, either I click "Allow" or "Don't Allow", it always bring me back to the login page, is there anything wrong?

    ReplyDelete
  6. Hi Steve,
    Usually if fails to authenticate if the device/emulator is having trouble with the ssl certificate.

    I just checked with the Nokia s40 emulator and it works fine - http://www.developer.nokia.com/Develop/Java/

    ReplyDelete
  7. Hi, I am facing problem with LWUIT1.5.
    Running in the identified_third_party security domain
    java.lang.NoClassDefFoundError: com/sun/lwuit/Display
    at userclasses.MainMIDlet.startApp(+1)
    at javax.microedition.midlet.MIDletProxy.startApp(MIDletProxy.java:44)
    at com.sun.midp.midlet.Scheduler.schedule(Scheduler.java:375)
    at com.sun.midp.main.Main.runLocalClass(Main.java:477)
    at com.sun.midp.main.Main.main(+80)

    ReplyDelete
  8. LWUIT isn't included properly into your project for some reason, it needs to be packaged into the jar.

    ReplyDelete
  9. hey friends,
    i'm integrating facebook with J2ME using LWUIT 1.4 ..
    But problem in, USER Permission page.
    unable to see "Allow" & "Dont Allow"
    button ...
    So if anyone succeed in same ..please help me for.
    my ID nyerunkar@gmail.com

    Thanks & Regards
    Nilesh.

    ReplyDelete
  10. @Nilesh after the authentication occurs you should show your own Forms

    ReplyDelete
  11. How do we find user is loged in successfully.Is any event occurs?

    Thanks,
    Jagan

    ReplyDelete
  12. This method

    FaceBookAccess.getInstance().authenticate(...);

    is blocking until authentication finishes.

    Then you can ask:

    FaceBookAccess.getInstance().isAuthenticated()

    ReplyDelete
  13. I am checking facebook implementation Demo app in lwuit. After login to facebook it reditects me to Allow and Don'tAllow page. After pressing Allow it is again sending me login page and this is happening every time, when i m pressing DontAllow it again redirect me to login page.In that Allow and Don't Allow page if i am selecting my AppName which is showing on top,I am able to enter the facebook but in that page which one is Selected i am unable to find out.Please help me and give me some suggestion as soon as possible.

    Thanks & Regards

    ReplyDelete
  14. and if i am trying to Open FaceBookDemo.res file through Resource Editor it is showing me some exception " An error occured while trying to load the file: java.io.IOException:Corrupt theme file unrecognized magic number:ee "

    ReplyDelete
  15. Hi Chen, I have checked,the control is still with FaceBookAccess.getInstance().authenticate() and it is unable to come out of that function and because of that every time when i am giving my login id and password and it redirects me to Allow and Don'tAllow page and after pressing any one of them it again send me login page.Please help i am stuck over there.

    ReplyDelete
  16. I am facing the same problem as others. The moment I click on "Allow" button of the Facebook Permission page, I am redirected to the login page. I have crossed checked by following the same steps on the web browser and it seems to me that since the page has javascripts calls in it, the API is not able to handle "onClick" method. Is the assumption right?

    ReplyDelete
  17. There isn't much we can do here. We intend to offer a completely rewritten solution that should solve these issues by using a shared cloud infrastructure in the future. But this will take some time.
    It will be a part of Codename One.

    ReplyDelete
  18. sorry for distrubing you but i just want to share the articel about J2ME
    let's check this link :
    http://repository.gunadarma.ac.id/bitstream/123456789/2725/1/16.pdf
    i wish that article can be usefull

    ReplyDelete
  19. That is why Facebook applications developers are now in demand specially to those who work online to increase their products of interests to provide more details of their business.

    ReplyDelete