def options(): parser = import_module(os.getenv('PARSER')).parse_options() o = parser.parse_args() logging.config.fileConfig(fileloc(o.log_config)) config = ConfigParser.SafeConfigParser() config.read(fileloc(o.config)) # parse config in-order keys = [] for section in config.sections(): for param in config.options(section): keys.append(param) setattr(o, param, config.get(section, param)) # cli arguments override configs for key in provided_arguments(parser): setattr(o, key, getattr(parser.parse_args(), key)) # environment variables override configs for key in keys: setattr(o, key, os.getenv(key.upper(), getattr(o, key))) return o
def get_credentials(scopes, email, options, storage_file="a_credentials_file"): if email: credentials = get_service_account_credentials( scopes=scopes, user_email=email, pkcs12_file_path=options.service_account_pkcs12_file_path, service_account_email=options.service_account_email, ) else: credentials = ensureOAuthCredentials(scopes=scopes, storage_file=fileloc(storage_file)) return credentials
def get_credentials(scopes, email, options, storage_file='a_credentials_file'): if email: credentials = get_service_account_credentials( scopes=scopes, user_email=email, pkcs12_file_path=options.service_account_pkcs12_file_path, service_account_email=options.service_account_email, ) else: credentials = ensureOAuthCredentials( scopes=scopes, storage_file=fileloc(storage_file)) return credentials
def ensureOAuthCredentials( secrets_file="client_secrets.json", storage_file="a_credentials_file", redirect_uri="https://localhost:8000/oauth2callback", scopes=[], ): storage = Storage(storage_file) credentials = storage.get() if not credentials: flow = flow_from_clientsecrets(filename=fileloc(secrets_file), scope=scopes, redirect_uri=redirect_uri) # Try to get refresh token in response. Taken from: # https://developers.google.com/glass/develop/mirror/authorization flow.params["approval_prompt"] = "force" auth_uri = flow.step1_get_authorize_url() print auth_uri code = raw_input("Auth token: ") credentials = flow.step2_exchange(code) storage.put(credentials) return credentials
def ensureOAuthCredentials( secrets_file='client_secrets.json', storage_file='a_credentials_file', redirect_uri='https://localhost:8000/oauth2callback', scopes=[]): storage = Storage(storage_file) credentials = storage.get() if not credentials: flow = flow_from_clientsecrets( filename=fileloc(secrets_file), scope=scopes, redirect_uri=redirect_uri, ) # Try to get refresh token in response. Taken from: # https://developers.google.com/glass/develop/mirror/authorization flow.params['approval_prompt'] = 'force' auth_uri = flow.step1_get_authorize_url() print auth_uri code = raw_input("Auth token: ") credentials = flow.step2_exchange(code) storage.put(credentials) return credentials
def get_service_account_credentials(scopes=[], user_email="", pkcs12_file_path="", service_account_email=""): with file(fileloc(pkcs12_file_path), "rb") as f: key = f.read() return SignedJwtAssertionCredentials(service_account_email, key, scope=scopes, sub=user_email)