Skip to content

Instantly share code, notes, and snippets.

@alanbernstein
Created December 2, 2015 18:54
Show Gist options
  • Save alanbernstein/ff4bccc833004dac5322 to your computer and use it in GitHub Desktop.
Save alanbernstein/ff4bccc833004dac5322 to your computer and use it in GitHub Desktop.
from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials
import json
import base64
import httplib2
def authorize(ga_email, ga_secret):
jwt = SignedJwtAssertionCredentials(ga_email, ga_secret, scope=self.ga_scope, private_key_password='notasecret')
http = jwt.authorize(httplib2.Http())
return build('analytics', 'v3', http=http) # failure happens here for JSON key file
# this works
ga_email = 'random@words.gserviceaccount.com'
with open('client_secrets.p12', 'rb') as f:
ga_secret = f.read()
service = authorize(ga_email, ga_secret)
# this fails
with open('client_secrets.json', 'r') as f:
json_data = json.load(f)
ga_email = json_data['client_email']
ga_secret_json = json_data['private_key']
ga_secret_b64 = ''.join(ga_secret_json.split('\n')[1:-2])
ga_secret_bin = base64.b64decode(ga_secret_b64)
ga_secret = ga_secret_bin # or ga_secret_b64 or ga_secret_json
service = authorize(ga_email, ga_secret)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment