- Goto https://code.google.com/apis/console
- Create a new project
- Under services, select Google Analytics
- Goto API Access of a new project
- generate a new oAuth credential, selecting “service account”.
- 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 ).
keyFile = "SOME_KEY_GOES_HERE-privatekey.p12"
cID = "YOUR_SOOPA_SECRET_SA"
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(
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.