There was once a time when Google was mainly associated with search, but today many of us use all sorts of Google services. The latest of these to draw a lot of attention is Google+ a relatively new social network which could maybe be described as a bit like Tumblr crossed with Facebook. Google offer APIs to many of these services and recently added a read only API to Google+. They also supply client libraries that are designed to work with traditional desktop environments but with a bit of modification can be made to work with the Scripting Layer for Android (SL4A). We need to modify the code in one of the program files that Google supply and change the location of some of the files, but after that you can go ahead and integrate all sorts of Google services with your scripts.
The developer site for Google+ can be found at: https://developers.google.com/+/api/. The Downloads link on this page offers you client libraries for many languages. In this case I am going to use Python so click on the link for the google-api-python-client and go to the Downloads page and click on the ZIP file for latest version of the google-api-python-client. At the time of writing this was google-api-python-client-1.0beta4.zip. Using an app such as File Station Tablet go to the downloads folder on your device and unzip it, this should give you a new folder in your downloads directory called something like google-api-python-client-1.0beta4.
Now we can copy what we need out of this folder into the Python installation on SL4A. Copy the following files and folders to /sdcard/com.googlecode.pythonforandroid/extras/python/:
This, in theory, gives us the tools we need to access many Google APIs from SL4A. However there is one slight issue. Google uses Oauth to enable client programs to be able to access data from their services. When a client program wants to gain access to information the user must be able to authorise this access request and then be able to go back to running the client program. The downloaded client library has a mechanism for providing a link that will go to a web page to authorise this access request and then will attempt to pass the Oauth token back to the script running on your machine, but this mechanism does not work well under SL4A so we need to alter it slightly to make using any scripts we create as easy as possible for the user.
Using a text editor app such as Jota we need to only modify one file to make the client library work smoothly with SL4A. Start up Jota and open the file /sdcard/com.googlecode.pythonforandroid/extras/python/oauth2client/tools.py. This is the file that handles the process of exchanging the Oauth token. Towards the top of the file, at the end of all of the import statements add another one:
Now we need to enable this script to launch a browser in which to show the permission dialogue and at the same time run a small local web server which will be used to receive the Oauth token being passed back from Google once the permission has been given. Normally this would be handled by the script outputting a URL on the screen which the user then clicks on to launch their browser in a separate process, but this does not work in the context of SL4A and the alternative used to be to type in the Oauth token manually which is not desirable either. If we added a straightforward call through the SL4A API to launch a browser we would be faced with the problem this this call would block the rest of the script until the browser window was closed which would stop the receiving web server from running, so we need to launch the browser in a separate thread so that the two processes can run at the same time. To do this we will add a new class that inherits from Python's Thread class. Add this new class below the code for the ClientRedirectServer() class:
# Run a browser in its own thread
_droid = None
_url = ""
def __init__(self, droid, url):
self._droid = droid
self._url = url
self._droid.view(self._url, None, None)
Now we have our browser thread code we can add in the code to launch it during the Oauth authentication process. Find the line that says:
print 'Go to the following link in your browser:' and add in these three new lines below it to launch a browser in its own thread to show the contents of the authorisation URL that has been generated:
droid = android.Android()
browser = BrowserThread(droid, authorize_url)
Our changes to this file are complete so save the file. We now need an example script to test that we are able to communicate with Google+ from SL4A. Fortunately we don't need to start from scratch, instead the Google+ Python API Starter Project provides us with a sample script that we can use to check everything is working. Create a new folder under /sdcard/sl4a/scripts/ called googleplus. Download and extract the zip file from the downloads page for that project and copy the contents of the cli folder within it to a new folder to /sdcard/sl4a/scripts/googleplus. Launch SL4A and we are almost ready to communicate with Google+ but there is one final bit of configuration to go.
In SL4A's Script Manager navigate to the googleplus folder created earlier and open up the settings.py file for editing. You will notice that we need to set up authorisation for the script to communicate with Google+. Some instructions are given at the top. You will need to go to the Google APIs console in a browser and follow the instructions to create a new project. Once you have done that you should be able to go the API access tab to create a new client ID, once this has been done, copy the client id and client secret values in the Client ID for installed applications section and the API key under the Simple API Access section to the right places in your settings.py file. Click on the Services tab and ensure that the Google+ API entry is set to ON. Save and exit the settings.py script.
All being well we should be able to run the sample script. Open up the plus_cli.py script in SL4A and run it. The first time you do this this a browser window should launch asking you to authorise the application, if you consent you should see a message saying "The authentication flow has been completed" (this is generated by the tools.py file we altered earlier). Switch back to SL4A using the task switcher or the tray icon and hopefully you should see some text that has been retrived from Google+!
As well as being able to work with the evolving Google+ API it should also be possible to work with many other Google service APIs as the library we installed was common to many of their services. The process to get this up and running was a little bit tricky but hopefully the ability to work with Google APIs in your SL4A scripts will be worth it.
Photo: Crop Circle by .thana