gapiclient.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:kumodocs 作者: kumofx 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号