Created
January 13, 2016 03:40
-
-
Save rdpickard/8da9f83b5dd98edf3176 to your computer and use it in GitHub Desktop.
Loading Google API JSON service account credentials in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import json | |
from oauth2client.client import SignedJwtAssertionCredentials | |
# P The Google API Using OAuth 2.0 for Server to Server Applications documentation | |
# P only seems to document using .p12 files. This is a snippet for openeing the suggested | |
# P JSON format key file | |
# P https://developers.google.com/api-client-library/python/auth/service-accounts#jwtsample | |
def google_get_credentials_from_serviceaccount(serviceaccount_credentials_filepath, | |
apis=list(), | |
logger=logging.getLogger()): | |
logger.debug("getting service account credentials from [{}]".format(serviceaccount_credentials_filepath)) | |
if not os.path.isfile(serviceaccount_credentials_filepath): | |
raise RuntimeError("No such service account credentials file {}".format(serviceaccount_credentials_filepath)) | |
service_account_credentails = json.load(open(serviceaccount_credentials_filepath)) | |
credentials = SignedJwtAssertionCredentials(service_account_credentails['client_email'], | |
service_account_credentails['private_key'], | |
apis) | |
return credentials | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment