Blog

/EDIT: Updated to check the audience and optionally clientid

Google’s Tim Bray recently published an excellent article (here) on using the new Google Play Services APIs to authenticate requests from Android devices using the Google account on the device.

The article contained Java, Ruby and PHP examples for the server side but unfortunately no python so here is my extremely naive implementation but it should be enough to give you a leg up.

It requires requests and PyJWT

import json
import jwt
import requests

GOOGLE_CERTS_URI = 'https://www.googleapis.com/oauth2/v1/certs'


class GoogleIdToken(object):
    def __init__(self):
        self._certs = {}
        self._token = {}

    def getCerts(self):
        """
        Grab the certificats from Google to decrypt the JWT token
        This really should cache the certs
        """
        cert = requests.get(GOOGLE_CERTS_URI)
        if cert.status_code == 200:
            return json.loads(cert.content)

    def isValid(self, token, audience, clientId=None):
        """
        Try each key in turn until we find one that decrypts the token
        """
        self._certs = self.getCerts()
        for key in self._certs:
            try:
                token = jwt.decode(token, key=self._certs[key], verify=False)
                if 'email' in token and 'aud' in token:
                    if token['aud'] == audience and (clientId == token['cid'] if clientId is not None else True):
                        self._token = token
                        return True
            except Exception, e:
                print "Error decoding: %s" % e.message
        return False

So, I had an issue with the EXCELLLENT!!!! soundmanager2′s whileloading event not firing in webkit based browsers. My solution is django based but should work for anyone having this issue.

I installed django-sendfile and Apache’s mod-xsendfile and added the following to my sites config file

XSendFile on

XSendFilePath /path/to/my/audio/files/

I needed to parse some stream details from an Icecast server for a personal project that I’m working on.
I couldn’t find anything readily so I knocked up a quick script to do it, should be fairly self-explanatory but let me know if it isn’t.

https://github.com/fergalmoran/pyicequery

Here’s one that was bugging me for ages, setting the width child items of a horizontal LinearLayout using percentages without 10/15 minutes of trial and error.
Set the weightSum of the layout to 100 and the layout_width of each of the the children to 0px then you can set the layout_weight attribute of each of the children to the percentage width you want them to have. Shaboom!

<LinearLayout
	android:orientation="horizontal"
	android:id="@+id/toprow"
	android:weightSum="100"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content">
		<TextView 
		  android:id="@+id/text1"
			android:layout_width="0px"
			android:layout_weight="80"
			android:layout_height="wrap_content"/>
		<TextView android:id="@+id/custom_property"
			android:layout_width="0px"
			android:layout_weight="20"
			android:layout_height="wrap_content"/>
</LinearLayout>