/** * Copyright (c) 2013 Nokia Corporation. All rights reserved. * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. * Oracle and Java are trademarks or registered trademarks of Oracle and/or its * affiliates. Other product and company names mentioned herein may be trademarks * or trade names of their respective owners. * See LICENSE.TXT for license information. */ package com.nokia.example; import javax.microedition.content.Invocation; import javax.microedition.content.Registry; import javax.microedition.content.ResponseListener; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; /** * The main class of the application. */ public class OAuthDemo extends MIDlet implements CommandListener, ResponseListener { private static OAuthDemo instance; private Form form; private Command authenticateCommand; private Command exitCommand; private StringItem infoItem; private StringItem tokenItem; /** * Creates and shows the main UI of the application. * Also adds this class as listener for the content handler. * @see javax.microedition.midlet.MIDlet#startApp() */ public void startApp() { if (instance == null) { instance = this; form = new Form("OAuthDemo"); authenticateCommand = new Command("Authenticate", Command.SCREEN, 1); exitCommand = new Command("Exit", Command.EXIT, 1); form.addCommand(authenticateCommand); form.addCommand(exitCommand); form.setCommandListener(this); infoItem = new StringItem( "Authentication example", "Press 'Authenticate' button to test MS Live authentication."); tokenItem = new StringItem("Token:", "-"); form.append(infoItem); form.append(tokenItem); Display.getDisplay(this).setCurrent(form); Registry.getRegistry(this.getClass().getName()).setListener(this); } } /** * @see javax.microedition.midlet.MIDlet#pauseApp() */ public void pauseApp() { // to be implemented } /** * @see javax.microedition.midlet.MIDlet#destroyApp() */ public void destroyApp(boolean unconditional) { // to be implemented } /** * @see javax.microedition.lcdui.CommandListener#commandAction * @param c a <code>Command</code> object identifying the command. * @param d the <code>Displayable</code> on which this event has occurred */ public void commandAction(Command c, Displayable d) { if (c == authenticateCommand) { createInvocation(); } if (c == exitCommand) { this.notifyDestroyed(); } } /* In order to use MS Live authentication, the app needs to be registered * and the client id for the API needs to be obtained. * Information about how to register the app and obtain the client id can be found from: * http://msdn.microsoft.com/en-us/library/bb676626.aspx */ 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"; /** * Creates Oauth invocation */ 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 listener is notified to indicate that an Invocation response is available. * @param registry the Registry with a new response */ 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 */ } } /** * Parses authentication token from URL * @param input input URL * @return authentication token */ 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; } }