Skip to content

Instantly share code, notes, and snippets.

@davisagli
Last active May 19, 2021 00:24
Show Gist options
  • Save davisagli/1128383064b1ba5c06b4 to your computer and use it in GitHub Desktop.
Save davisagli/1128383064b1ba5c06b4 to your computer and use it in GitHub Desktop.
authenticating to google API using Service Account credentials
import httplib2
# these are from the google-api-python-client package
from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials
def _get_google_service(email, key):
"""Get a Google API client using Service Account credentials"""
credentials = SignedJwtAssertionCredentials(
email, key, scope='https://www.googleapis.com/auth/analytics.readonly')
http = httplib2.Http()
http = credentials.authorize(http)
service = build('analytics', 'v3', http=http)
return service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment