Python googleapiclient.discovery() Examples

The following are 9 code examples for showing how to use googleapiclient.discovery(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module googleapiclient , or try the search function .

Example 1
Project: streamalert   Author: airbnb   File:    License: Apache License 2.0 6 votes vote down vote up
def _load_credentials(cls, keydata):
        """Load ServiceAccountCredentials from Google service account JSON keyfile

            keydata (dict): The loaded keyfile data from a Google service account
                JSON file

             google.oauth2.service_account.ServiceAccountCredentials: Instance of
                service account credentials for this discovery service
            creds = service_account.Credentials.from_service_account_info(
        except (ValueError, KeyError):
            # This has the potential to raise errors. See:
            LOGGER.exception('[%s] Could not generate credentials from keyfile', cls.type())
            return False

        return creds 
Example 2
Project: shoogle   Author: tokland   File:    License: GNU General Public License v3.0 5 votes vote down vote up
def run(options):
    """Run command execute."""
    service_id, resource_name, method_name = lib.pad_list(options.api_path.split(".", 2), 3)
    request_fd = (sys.stdin if options.json_request == "-" else open(options.json_request))
    method_options = lib.load_json(
        response = do_request(service_id, resource_name, method_name, method_options, options)
    except TypeError as error:
        frm = inspect.trace()[-1]
        mod = inspect.getmodule(frm[0])
        if mod.__name__ == 'googleapiclient.discovery':
            config.logger.error("googleapiclient.discovery: {}".format(error))
Example 3
Project: shoogle   Author: tokland   File:    License: GNU General Public License v3.0 5 votes vote down vote up
def build_service(service_id, credentials):
    """Return service object from ID and credentials."""
    base_http = httplib2.Http()
    http = (credentials.authorize(base_http) if credentials else base_http)
    service_name, version = service_id.split(":", 1)
    return, version, http=http) 
Example 4
Project: streamalert   Author: airbnb   File:    License: Apache License 2.0 5 votes vote down vote up
def _create_service(self):
        """GSuite requests must be signed with the keyfile

            bool: True if the Google API discovery service was successfully established or False
                if any errors occurred during the creation of the Google discovery service,
        LOGGER.debug('[%s] Creating activities service', self)

        if self._activities_service:
            LOGGER.debug('[%s] Service already instantiated', self)
            return True

        creds = self._load_credentials(self._config.auth['keyfile'])
        if not creds:
            return False

        delegation = creds.with_subject(self._config.auth['delegation_email'])
            resource =
        except self._GOOGLE_API_EXCEPTIONS:
            LOGGER.exception('[%s] Failed to build discovery service', self)
            return False

        # The google discovery service 'Resource' class that is returned by
        # '' dynamically loads methods/attributes, so pylint will complain
        # about no 'activities' member existing without the below pylint comment
        self._activities_service = resource.activities()  # pylint: disable=no-member

        return True 
Example 5
Project: recsys2019   Author: logicai-io   File:    License: Apache License 2.0 5 votes vote down vote up
def main(config_file, validation):
    compute ="compute", "v1")
    project = "logicai-recsys2019"
    timestamp = get_timestamp()
    zone = "europe-west1-b"
    snapshot_name = "recsys1-models"
    instance_name = f"recsys-tmp-{timestamp}"
    validation_str = "val" if validation else "sub"
    disk_name = f"recsys-tmp-disk-{timestamp}"
    with open(config_file) as inp:
        model_config =
    config_hash = str(str_to_hash(model_config) % 1_000_000)
    storage_path = f"predictions/runs/{config_hash}_{timestamp}_{validation_str}/"
    # make sure it is a proper json
    assert json.loads(model_config)
    validation = 1 if validation else 0
    print("Clone disk from snapshot")
    operation = clone_disk_from_snapshot(
        compute=compute, project=project, zone=zone, snapshot_name=snapshot_name, disk_name=disk_name
    print("Waiting for creation")
    wait_for_operation(compute, project, zone, operation["name"])
    print("Create instance")
    operation = create_instance(
    wait_for_operation(compute, project, zone, operation["name"])
    print("Waiting some time for the network")
    os.system(f"gcloud compute scp pawel@{instance_name}:/tmp/")
    os.system(f"gcloud compute ssh {instance_name} --command='chmod +x /tmp/'")
    os.system(f"gcloud compute ssh {instance_name} --command='nohup /tmp/ &'") 
Example 6
Project: hyou   Author: google   File:    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, http, discovery):
        if discovery:
            self.sheets =
                'sheets', 'v4', http=http,
                'drive', 'v2', http=http)
            self.sheets = googleapiclient.discovery.build_from_document(
                schema.SHEETS_V4, http=http)
   = googleapiclient.discovery.build_from_document(
                schema.DRIVE_V2, http=http) 
Example 7
Project: cloud-debug-python   Author: GoogleCloudPlatform   File:    License: Apache License 2.0 5 votes vote down vote up
def _BuildService(self):
    http = httplib2.Http(timeout=_HTTP_TIMEOUT_SECONDS)
    http = google_auth_httplib2.AuthorizedHttp(self._credentials, http)

    api =
        'clouddebugger', 'v2', http=http, cache_discovery=False)
    return api.controller() 
Example 8
Project: professional-services   Author: GoogleCloudPlatform   File:    License: Apache License 2.0 4 votes vote down vote up
def migrate_instance_to_cmek(project, zone, instance, key_ring, key_name,
                             key_version, destructive):
  start = time.time()

  zone_regexp = r'^(\w\w-\w*\d)-(\w)$'
  region =, zone).group(1)

  compute ='compute', 'v1')

  stop_instance(compute, project, zone, instance)
  disks = get_instance_disks(compute, project, zone, instance)
  for source_disk in disks:
    disk_regexp = r'^https:\/\/www\.googleapis\.com\/compute\/v1\/projects\/(.*?)\/zones\/(.*?)\/disks\/(.*?)$'
    disk_url = source_disk['source']
    existing_disk_name =, disk_url).group(3)

    if 'diskEncryptionKey' in source_disk:'Skipping %s, already encrypyed with %s', existing_disk_name,

    snapshot_name = '{}-goog-to-cmek'.format(existing_disk_name)
    new_disk_name = '{}-cmek'.format(existing_disk_name)
    disk_type = get_disk_type(compute, project, zone, existing_disk_name)

    create_snapshot(compute, project, zone, existing_disk_name, snapshot_name)
    key_name = 'projects/{0}/locations/{1}/keyRings/{2}/cryptoKeys/{3}/cryptoKeyVersions/{4}'.format(
        project, region, key_ring, key_name, key_version)
    create_disk(compute, project, region, zone, snapshot_name, new_disk_name,
                disk_type, key_name)
    detach_disk(compute, project, zone, instance, existing_disk_name)

    boot = source_disk['boot']
    auto_delete = source_disk['autoDelete']
    attach_disk(compute, project, zone, instance, new_disk_name, boot,
    if destructive:
      delete_disk(compute, project, zone, existing_disk_name)
      delete_snapshot(compute, project, snapshot_name)

  start_instance(compute, project, zone, instance)

  end = time.time()'Migration took %s seconds.', end - start) 
Example 9
Project: cloud-debug-python   Author: GoogleCloudPlatform   File:    License: Apache License 2.0 4 votes vote down vote up
def __init__(self):
    self.on_active_breakpoints_changed = lambda x: None
    self.on_idle = lambda: None
    self._debuggee_labels = {}
    self._service_account_auth = False
    self._debuggee_id = None
    self._agent_id = None
    self._canary_mode = None
    self._wait_token = 'init'
    self._breakpoints = []
    self._main_thread = None
    self._transmission_thread = None
    self._transmission_thread_startup_lock = threading.Lock()
    self._transmission_queue = deque(maxlen=100)
    self._new_updates = threading.Event()

    # Disable logging in the discovery API to avoid excessive logging.
    class _ChildLogFilter(logging.Filter):
      """Filter to eliminate info-level logging when called from this module."""

      def __init__(self, filter_levels=None):
        super(_ChildLogFilter, self).__init__()
        self._filter_levels = filter_levels or set(logging.INFO)
        # Get name without extension to avoid .py vs .pyc issues
        self._my_filename = os.path.splitext(

      def filter(self, record):
        if record.levelno not in self._filter_levels:
          return True
        callerframes = inspect.getouterframes(inspect.currentframe())
        for f in callerframes:
          if os.path.splitext(f[1])[0] == self._my_filename:
            return False
        return True
    self._log_filter = _ChildLogFilter({logging.INFO})

    # Configuration options (constants only modified by unit test)

    # Delay before retrying failed request.
    self.register_backoff = backoff.Backoff()  # Register debuggee.
    self.list_backoff = backoff.Backoff()  # Query active breakpoints.
    self.update_backoff = backoff.Backoff()  # Update breakpoint.

    # Maximum number of times that the message is re-transmitted before it
    # is assumed to be poisonous and discarded
    self.max_transmit_attempts = 10