Mar 16, 2011

Facebook and Java ME: How to authenticate?

Hi all,

Last month, I have developed a tiny API that integrates a Java ME app into Facebook. The implementation was not a big deal, except the authentication process. Since Facebook uses OAuth 2.0, it requires that any app that wants to integrate into Facebook's services must authenticate via its web login page. We all know that displaying HTML page in Java ME is not a easy task, since we have no LCDUI component that renders HTML. Fortunately, LWUIT already provides that via HTMLComponent.

But how to accomplish that if my app is not based on LWUIT? Hard question, huh? In my case, fortunately, the app that I had to integrate into Facebook is based on eSWT, which provides a Browser component. Because that, I was able to access the Facebook's login page and track the whole process in order to retrieve the token access required to access the methods from Facebook API.

My point in this post is: how to integrate Java ME app into Facebook API without a browser component? What is the trick? Parser the web login page's content and simulate the form submission programmatically? How have you guys have done that?

In intend to release my Facebook API in a near future. So I would like to know a better approach to implement the authentication process, so this API do not get tied to any specific framework.

See you in the next post...

11 comments:

Steve Zhang said...

Hi, I tested on Nokia E72 device, it it is not working to access the facebook Oath URL. could you send me your sample code for facebook login?
Thanks

Ernandes Mourão Júnior said...

Hi, Steve

Unfortunately, at this moment I a, not allowed to release any code. On the other hand, I could help you to get your code running. Are you using eSWT or LWUIT to display the Facebook's login page? What is your issue?

Regards,
Ernandes

Steve Zhang said...

Hi, Ernandes,

I just use this sample code to test the facebook login:
http://wiki.forum.nokia.com/index.php/CS000974_-_Using_eSWT_Browser_in_Java_ME

and I put the URL:
"https://m.facebook.com/dialog/oauth?type=user_agent&redirect_uri=http://www.facebook.com/connect/login_success.html&client_id=115523051848827&scope=read_stream,publish_stream,offline_access,friends_birthday,user_birthday,user_photos,friends_photos&display=wap"


When I run the application, it first display the facebook login page, ask me type the email name and password, after I press "login" button, the browser display an error.

Ernandes Mourão Júnior said...

Hi,

According to the documentation, I just call the following URL and parameters, for instance:

http://www.facebook.com/dialog/oauth?client_id=1234567890&redirect_uri=http://www.emobtech.com/&display=wap&scope=publish_stream,offline_access

I believe your issue is related to "redirect_uri" parameter. This uri must be same as you defined when you registed your app on your Facebook's account. Try to use a simple and short uri, just like your company's or personal page, for instance.

I hope it works out for you.

Regards,
Ernandes

Steve Zhang said...

redirect_uri should be fine, because I login successfully using the LWUIT browser, the thing is its jar size is too big

Minhaj said...

Hi there!

I'm working on a product and unfortunately am stuck at the same spot as your last blog. My application uses a platform which doesn't support browser. To make the product integrate I want to authenticate the application without using the browser.

Pls share your thoughts.

Ernandes Mourão Júnior said...

Hi,

Unfortunately, there is no way out: you do need a browser to authenticate on Facebook. Some apps that require user registration on a website have offered an option for users to connect their accounts to Facebook, Twitter, etc. This way, they use their on server infrastructure to access Facebook. It is just like Foursquare and other user-based apps available in the market.

Regards,

Minhaj said...

Thanks Ernandes,

My problem is that the application runs a phone without internet and uses SMS in the background to send and receive updates. What are my options?

Rgds

Minhaj said...

Also i came across some products where the user can update posts using mediums such as sms. The app is authenticated by texting "allow" and In the background ,shortly after texting "allow", i see the application for the product is added to my application list.

Ernandes Mourão Júnior said...

It is harder than I could imagine. :)
Indeed. There are products like that. Roughly, what they do is to implement a service that listens to SMS. If this SMS represents a message to be posted on Facebook, this service would do it.

Minhaj said...

That is exactly where i'm stuck :)
That is, to post message on FB when i receive it from the app. But to do so the user must authenticate. aaaaaarrrrggggg!!! pardon my frustration.