Alternative Twitter Authentication via Geominer

Alternative Twitter Authentication via Geominer

It would have been wonderful if we could have used the Twitter Anywhere API for our application, but, as we want to package the application for marketplace distribution using PhoneGap, that is off the table—at least for the moment. In this section, we will investigate implementing some alternative authentication using some helpful parts of the Geominer API.

Let’s start by including the Geominer JavaScript library into index.html, replacing the previous Twitter Anywhere references:

<!DOCTYPE html> <html> <head> ... <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"> </script>

<script type="text/javascript" src="http://api.geominer.net/jsapi/v1/geominer.js"> </script>

<script type="text/javascript"> function initialize() {

MOUNDZ.init();

} // initialize </script> </head> <body onload="initialize()"> ... </body> </html>

Next, let’s wire the Geominer initialization the MOUNDZ.init function: MOUNDZ = (function() {

// initialize variables var geominer = null,

... posWatchId = 0;

CHAPTER 10: Integrating with Social APIs

function gotoPosition(position, zoomLevel) { // define the required options var myOptions = {

zoom: zoomLevel ? zoomLevel : DEFAULT_ZOOM, center: position, mapTypeControl: false, streetViewControl: false, mapTypeId: google.maps.MapTypeId.ROADMAP

}; // initialize the map

map = new google.maps.Map(

document.getElementById("map_canvas"), myOptions);

} // gotoPosition

... var module = {

... init: function(zoomLevel) {

// initialize the geominer bridge geominer = new GEOMINER.Bridge({

app: 'moundz', login: '#login'

}); $(geominer).bind('authenticated', function(evt) {

$('#splash').hide(); $('#app').show();

run(zoomLevel);

Download from Wow! eBook <www.wowebook.com>

// initialize the screen initScreen();

return module; })();

We have made a few changes to the MOUNDZ module here, which in addition to wiring in the Geominer JavaScript API helps to reorganize things a little:

1. The previous contents of the MOUNDZ.init function have been moved to a new internal function called gotoPosition.

2. A new module variable, geominer, is defined at the beginning of the module.

CHAPTER 10: Integrating with Social APIs

3. The geominer variable is initialized with a new GEOMINER.Bridge object as part of the refined init function. At this stage, the bridge is created and two parameters are specified—the name of the app that we are running now (the Geominer API has been built to support multiple apps) and the login container that will receive the login button (as per the Twitter Anywhere initialization).

4. Finally, we use the jQuery bind function to listen for “authenticated” events, which flag to us that a user has completed the login process. In response to this event, we will close the welcome screen and display the main application view.

Very little code is actually required to implement the connectivity within the MOUNDZ module, and this is due to the behind-the-scenes work that the Geominer API is doing for us. If you are interested, we encourage you to check out the source code for the Geominer JavaScript API, at http://api.geominer.net/jsapi/v1/geominer.js.

If everything has gone according to plan, we should have a slightly different login experience now. Figure 10–18 shows the updated login screen, which is quite similar in appearance to the Twitter Anywhere version.

Figure 10–18. The welcome screen using the Geominer API While the initial welcome screen looks similar, the process will differ from this point

forward. Figure 10–19 shows the mobile-optimized permissions screen that we receive while using the established server-side APIs (via Geominer) rather than Twitter Anywhere.

CHAPTER 10: Integrating with Social APIs

Figure 10–19. Using the more established Twitter RESTful API provides a better mobile experience. Given that we accept the connection to Twitter, we will be returned to our application via

the Geominer API. While this is going on, the client-side Geominer API script that we included is monitoring the login process, and, once it has determined that the process has completed successfully, the welcome screen is hidden and the user is taken to the main application screen as per earlier in the application.

NOTE: One aspect of this solution that is not as elegant as using Twitter Anywhere is that the application is shown as “Geominer” rather than “Moundz” when we are going through the authentication process. While future versions of Geominer may use different OAuth parameters to show the request as coming from an application rather than itself, the current version doesn’t.

If you are looking to build an application that is not a geosocial game, but you are interested in how we implemented the alternative login mechanism using Geominer, then it is probably worthwhile to fork the Geominer project on GitHub, which can be found at the following URL: https://github.com/sidelab/geominer.