def start(self, service_name, scope='https://www.googleapis.com/auth/drive'):
"""
Reads config file and initializes the GSuite API client with proper authentication
:param service_name: Name of service to start, one of gsuite.SERVICES
:param scope: API scope to authorize. Defaults to read/manage files in Google Drive.
:return: Google API client for making requests.
"""
logger.info('Creating the client service')
tokens, client_secrets = KIOutils.get_abs_config_path()
flow = oa_client.flow_from_clientsecrets(client_secrets,
scope=scope,
message=oa_tools.message_if_missing(client_secrets))
storage = oa_file.Storage(tokens)
credentials = storage.get()
if credentials is None: # or credentials.invalid:
if self.has_client_secrets(client_secrets):
credentials = oa_tools.run_flow(flow, storage, flags=None)
else:
raise NotImplementedError(oa_tools.message_if_missing(client_secrets))
# noinspection PyBroadException
try:
http = credentials.authorize(httplib2.Http())
client = googleapiclient.discovery.build(serviceName=service_name, version="v2", http=http,
cache_discovery=False)
client.http = http # directly expose http without using 'protected' _http
except Exception:
logger.error('Failed to create service', exc_info=True)
raise sys.exit(1)
else:
logger.info('Created and authorized the client service')
return client
评论列表
文章目录