×
Namespaces

Variants
Actions

How to connect to Facebook using CodenameOne on Series 40

From Nokia Developer Wiki
Jump to: navigation, search

This article explains how to create a Facebook based application for series 40 using CodenameOne

Article Metadata
Tested with
Devices(s): Asha 311
Compatibility
Platform(s): Series 40 Developer Platform 2.0
Series 40
Series 40 DP 2.0
Device(s): All Series 40
Article
Keywords: facebook, codenameone
Created: fishfishb (24 Apr 2013)
Last edited: hamishwillee (27 Jun 2013)

Contents

Introduction

Working with Facebook API in J2ME is a hard task because Facebook uses oAuth2 to authenticate the user, which requires showing a browser to the user and the ability to interact with the browser. On most platforms this is a easy task since most platforms provide a WebView API. Unfortunately J2ME doesn't provide a J2ME Web API which makes the above task very hard.

In this article we will demonstrate how to create Facebook connected apps using CodenameOne.

What is CodenameOne?

Codename One is a free open source solution that allows you to rapidly build native applications to all mobile platforms (J2ME, Android, iOS, BB, Win8 ) using Java & optionally a GUI builder.

CodenameOne provides IDE plugins for Netbeans & Eclipse, Simulator, GUI builder and cloud services.

The CodenameOne API provides a full set of capabilities such as: Rich UI (a fork of LWUIT), File, Contacts, Network, GPS, Camera, Media, Maps, barcode scan, analytics and much more...

Below is a quick video tutorial showing how to create a simple hello world in CodenameOne. The media player is loading...

Getting Started

  1. First follow this Getting Started With Codename One to install the plugin of your favourite IDE
  2. Go to the Facebook developers portal page: developers.facebook.com/apps
  3. Create your Facebook app by selecting Create New App on the top right
    Cn1 newFBApp.png
  4. Select mobile web for your app and save changes
    Cn1 webfb.png
  5. Now create a simple CodenameOne "Hello World" app in your Java IDE (Pictures are from NB)
    Cn1 new1.png
  6. Select Native Theme and Hello World(Manual) for simplicity
    Cn1 new2.png
  7. Now you can run your project from the IDE - a simulator will be launched with the default iPhone Simulator, to change the Simulator to Nokia Asha go to Skins->More... select the Asha skin and download it.
    The Asha skin will be added to the Skins menu, select it to change the skin.
    Cn1 skin.png
  8. Now write your code to authenticate with the Facebook app


The important class is FacebookAccess; you need to set it's properties to communicate with your Facebook entity. In my example this is the code:

        FaceBookAccess.setClientId("587443844606980");
FaceBookAccess.setClientSecret("473830f87b2afe06b874295cedfc9b88");
FaceBookAccess.setRedirectURI("http://www.codenameone.com/");
FaceBookAccess.setPermissions(new String[]{"user_location", "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"});
FaceBookAccess.getInstance().showAuthentication(new ActionListener() {
 
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() instanceof String) {
String token = (String) evt.getSource();
String expires = Oauth2.getExpires();
System.out.println("recived a token " + token + " which expires on " + expires);
} else {
Exception err = (Exception) evt.getSource();
err.printStackTrace();
Dialog.show("Error", "An error occurred while logging in: " + err, "OK", null);
}
}
});
Cn1 connect1.png

The showAuthentication() method will popup the authentication process once the user has authenticated you got the token now with the token you are able to query the FB Graph API.

References

For more information visit the below links:

This page was last modified on 27 June 2013, at 06:28.
337 page views in the last 30 days.