Tag Archives: woot

Service accounts with Ruby and the google api client

  1. Goto https://code.google.com/apis/console
  2. Create a new project
  3. Under services, select Google Analytics
  4. Goto API Access of a new project
  5. generate a new oAuth credential, selecting “service account”.
  6. Download your p12 cert file


In Google analytics, use the service account email address and assign it as a new administrator to the desired GA profile.

On ALL servers destined to use the service account to GA, install and syncronized with ntp. ┬áIf your even 500ms off, you’re going to have a bad time with “Grant_invalid” messages.

In a testbed project, make 2 files: Gemfile & testbed1.rb ( you’re going to create a lot of these testbed files on your own ).


source "http://rubygems.org"
gem 'google-api-client'
gem 'pry'


require 'pry'
require 'rubygems'
require 'google/api_client'
keyFile = "SOME_KEY_GOES_HERE-privatekey.p12"
scope = 'https://www.googleapis.com/auth/analytics.readonly'
email = 'YOUR_EMAIL_GOES_HERE@developer.gserviceaccount.com'
#Normally it would be bad to put the passphrase here, but aftet talking to several dozen devs, everyone's pass phrase is not a secret
key = Google::APIClient::PKCS12.load_key(keyFile, "notasecret")
asserter = Google::APIClient::JWTAsserter.new(
puts asserter.authorize()

If all goes well, the JWTAsserter instance will get a valid token and no exceptions/errors will be thrown. If you’ve followed all of the steps listed earlier and things are still breaking, troubleshooting paths are: Verify your machine time is correct, verify your service account email address is bound to google analytics, and lastly you might need to wait until Google platform catches up with the changes. For myself, it took 9 hours until my account finally authenticated through. Google has potentially a million or more servers organized into cells, the fact that they seem to cooperate well doesn’t mean they’re perfectly in sync at all times.

Otherwise if all else fails, I recommend stalking this guy Nick https://groups.google.com/d/msg/google-analytics-data-export-api/maa_fyjD2cM/sT8tDDh0wNsJ – It seems like he’s a Google employee on the service account dev/implementation team and generally stuff gets fixed if he says it’s getting fixed.

As I run into any more troubles, I will update and add more notes.

Ubuntu 11.10 Google native client dependancies

sudo apt-get install ia32-libs
sudo apt-get install gcc-multilib g++-multilib libsdl1.2-dev texinfo libcrypto++-dev libssl-dev lib32ncurses5-dev m4 libelf-dev

This is about 300Mb of stuff, some of it can be culled but I didn’t feel like messing around with that.

Allows for pepper 19 examples to be compiled successfully.

New language to the toolset: NodeJS

I’ve started self-teaching myself NodeJS and not surprisingly there was a recent popular reddit link pointing to a not very short introduction to nodejs here ( http://anders.janmyr.com/2011/05/not-very-short-introduction-to-nodejs.html ) . At this point I haven’t found any good books for reference lookup… but hopefully in a week or so I’ll have gotten spooled up enough to be able to report back.

In the meantime as I use Ubuntu for everything but games, this short article here ( http://www.codediesel.com/linux/installing-node-js-on-ubuntu-10-04/ ) was enough to get a working environment running though I recommend using a pristine virtual machine instance to keep things simple.

Last point, a reliable friend & peer of mine recommended I check out this MVC like framework for NodeJS ( http://expressjs.com/ ). Preliminary it feels like a hybrid between Twistd and CherryPy in Python or Limonade PHP as far as style… aiming more for simplicity over feature/complexity.

Semi-critical OpenSSL memory issue shared with Node.js and Twisted Python

I found this ( http://journal.paul.querna.org/articles/2011/04/05/openssl-memory-use/ ) just a tad disturbing. The TLDR is that OpenSSL automatically sets compression on for SSL connections. That’s great if you’re trying to cut down on bandwidth… but not so great when thinking about both Memory and CPU utilization, definitely more so memory in this case.

Fortunately the post author and company created some partial duct tape solutions for anyone suffering under out of memory issues in their infrastructure.

Python helping PHP development

Just a quick little utility script I wrote to make watching PHP error logs a tad easier:

Usage is

#tail -F /var/log/php/php_error.log | ~/bin/parsePHPErrors.py

Thanks to this Stackoverflow question, it even colorizes!

import sys
import os
import re
input = os.fdopen(sys.stdin.fileno(),'r',100)
regex = re.compile("(.*?PHP (Warning|Error|Notice|):.*)")
line = input.readline()
class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'    
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
myColor = bcolors.OKGREEN
while line:
    if regex.match(line) or line.find("TODO") > -1:
        isCorrect = True
        if line.find("Fatal error") > -1:
            myColor = bcolors.FAIL
        elif line.find("Error")  > -1:
            myColor = bcolors.FAIL
            #todo get console color code
        elif line.find("Warning")  > -1:
            myColor = bcolors.WARNING
        elif line.find("Notice")  > -1:
            myColor = bcolors.OKBLUE
        elif line.find("TODO") > -1:
            myColor = bcolors.OKBLUE
        if isCorrect:            
            print "#"*25, "@"*25, "#"*25
        #myColor = bcolors.OKGREEN
    print myColor, line.strip(), bcolors.ENDC
    line = input.readline()

Output looks like:

######################### @@@@@@@@@@@@@@@@@@@@@@@@@ #########################

[28-Mar-2011 11:14:10] PHP Notice:  Undefined variable: sql in

basically breaking up individual error messages into visually easy to spot blocks

Initial look at ExtJS 4

So… the graphing library look damn good, but focusing more on the nutts & bolts.

Previously ExtJS has had an on demand instantiation system vi anoymous objects with a property of xtype. It’s fairly likely that not every component of an ExtJS enhanced page is going to be used by a user or even looked at, so the xtype support allowed for avoiding costly and wasted object/component instantiations.

Now it looks like they’re taking that one step further. Looking at the Public Release 3 of ExtJS 4’s documentation, I noticed some new additions… specifically lazy class loading. If I’m correct in my assumptions, that means that instead of loading the ENTIRE ExtJS library and possibly only using a tiny bit of it, you can load up only what you need saving rendering time & bandwidth!

Ext.onReady(function() {
    var window = Ext.widget('window', {
        width: 500,
        height: 300,
        layout: 'fit',
        items: {
            xtype: 'button',
            text: 'Hello World',
            handler: function() { alert(this.text) }

Authoritative source here

1 Billion monthly pageviews

It’s been an odd journey these last few months, but Lijit just hit 1 billion monthly page views in the last week or so.


What get’s me though is that the page views value is probably substantially lower then the actual count of handled http requests a month. Wish I had the time to compute that number.