Implementation

For information about the design and functionality of the MIDlet, see section Design

The MIDlet consists of a single class that is used for displaying the Access Token. The MIDlet’s main screen features a Command that launches the authentication invocation and a StringItem that is reserved for displaying the Access Token. Since the main MIDlet class implements the invocation, it needs to implement the ResponseListener interface and also provide an implementation for the invocationResponseNotify method. The latter is the call back method that will be executed when a response to the invocation has been returned back to the MIDlet. The MIDlet needs to register the ResponseListener first. This happens within the startApp method as shown below:

public class OAuthDemo  
    extends MIDlet  
    implements CommandListener, ResponseListener { 
    public void startApp() { 
		    …  
		    Registry.getRegistry(this.getClass().getName()).setListener(this);         
    } 
    public void invocationResponseNotify(Registry registry) { 
        … 
    } 
}

Before creating an invocation, the developer needs to obtain a client id. The invocation URL can be broken down into the access URL that is used for accessing the remove service. The access URL contains the client id that helps the service identify the application. The invocation URL contains also the redirect URL that will be used to return back to the MIDlet. These are the declarations of the accessURL and redirectURL variables:

private static final String client_id = ""; // Please paste the Live id here 
// URL for the authentication service 
private static final String accessURL = "https://login.live.com/oauth20_authorize.srf?client_id=" 
    + client_id + "&scope=wl.basic,wl.offline_access&response_type=token&redirect_uri="; 
// Redirect URL for the authentication 
private static final String redirectURL = "https://login.live.com/oauth20_desktop.srf";

The next step is to create an Invocation. An invocation instance is used for this purpose that is passed as the single argument to the invoke method within a try catch block. The invocation URL is defined as the concatenation of the access URL with the redirect URL. The invocation URL is being passed to the invocation instance with the setURL method. There are also two arguments passed to the invocation instance, the first is the proxy mode and the second is the redirect URL. The complete invocation code as shown below is called when the user taps the Authenticate Command:

private void createInvocation() {
    Invocation inv = new Invocation();
    inv.setURL(accessURL + redirectURL);
    inv.setID("com.nokia.browser");
    inv.setArgs(new String[] { "mode=proxy", "redirect_intercept=" + redirectURL });
    inv.setResponseRequired(true);
    try {
        String my_class = this.getClass().getName();
        Registry.getRegistry(my_class).invoke(inv);
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

The response from the server is expected to contain the access token. The invocationResponseNotify call back method returns a Registry instance when the user has authenticated on the browser and has given the permission to the MIDlet to access some personal information. The response is being represented as an Invocation instance as well. The Invocation instance is extracted from the Registry instance with the getResponse method. The Invocation contains the status of the response and the response URL. The status of the response is read first and then the response URL is extracted from the Invocation instance with the getURL method.

public void invocationResponseNotify(Registry registry) {
    Invocation response = registry.getResponse(true);
    if (response.getStatus() == Invocation.OK) {
        String redirectedURL = response.getURL();
        /* parse URL for access token and show the token in the main view */
        String token = parseToken(redirectedURL);
        System.out.println("Token:" + token);
        this.tokenItem.setText(token);
    }
    else {
        System.out.println("GetStatus = " + response.getStatus());
        /* handle cancelled invocation */
    }
}

Parsing the reponse is implemented as a separate method. The access token is read from the response URL between the "#access_token=" and the "&" character following right after:

private String parseToken(String input) {
    if (input == null) {
        return null;
    }
    String accessToken = "#access_token=";
    int start;
    int end;
    start = input.indexOf(accessToken);
    if (start == -1) {
        return null;
    }
    else {
        start += accessToken.length();
    }
    end = input.indexOf("&", start);
    String token = input.substring(start, (end != -1) ? end : input.length());
    return token;
}