Google Ad Manager SOAP API Python Client Library

repository·main·Indexed 20 days ago

https://github.com/googleads/googleads-python-lib

A high-level Python interface for the Google Ad Manager SOAP API. The library handles OAuth2 authentication, YAML-based configuration, and SOAP web service client creation. It requires Python 3.7+ and supports WSDL caching configuration and SOAP interaction logging via the zeep library.

Tokens
1.2K
Snippets
5
Records
5
Agent score
23%

What's inside googleads-python-lib

  1. Initialize AdManagerClient from a configuration file

    main

    The library uses a googleads.yaml file to store credentials and settings. You can copy this file to your home directory for default loading, or provide a specific path to the LoadFromStorage method.

    1. Copy googleads.yaml to your home directory.
    2. Use AdManagerClient.LoadFromStorage() to initialize the client.
    # Use the default location - your home directory:
    ad_manager_client = ad_manager.AdManagerClient.LoadFromStorage()
    
    # Alternatively, pass in the location of the file:
    ad_manager_client = ad_manager.AdManagerClient.LoadFromStorage('C:\My\Directory\googleads.yaml')
  2. Install the googleads library

    main

    You can install the library and its dependencies using pip, which is the recommended method. Alternatively, you can install from a downloaded tarball using setup.py.

    Prerequisites:

    • setuptools must be installed.
    • Python 3.7+ is required.
    # Recommended method via PyPI
    $ pip install googleads
    
    # Alternative method via tarball
    $ python setup.py build install
  3. Disable log filters to see sensitive data

    main

    The zeep plugin used for logging automatically strips sensitive data. To include this data in your logs for debugging, you must implement and append a custom zeep.Plugin to the ad_manager_client.zeep_client.plugins list.

    import logging
    from lxml import etree
    import zeep
    
    class DangerousZeepLogger(zeep.Plugin):
      def ingress(self, envelope, http_headers, operation):
        logging.debug('Incoming response: \n%s', etree.tostring(envelope, pretty_print=True))
        return envelope, http_headers
    
      def egress(self, envelope, http_headers, operation, binding_options):
        logging.debug('Incoming response: \n%s', etree.tostring(envelope, pretty_print=True))
        return envelope, http_headers
    
    ad_manager_client.zeep_client.plugins.append(DangerousZeepLogger())
  4. Log SOAP interactions

    main

    The library uses Python's built-in logging framework. You can configure logging via the googleads.yaml file or manually in your code. To log SOAP interactions specifically using the zeep library, set the level for googleads.soap to DEBUG.

    import logging
    
    # Log SOAP interactions to stdout
    logging.basicConfig(level=logging.INFO, format=googleads.util.LOGGER_FORMAT)
    logging.getLogger('googleads.soap').setLevel(logging.DEBUG)
  5. Configure or disable WSDL caching

    main

    By default, the library caches WSDLs to improve performance. If you are in an environment like App Engine where local file system access is restricted, you can provide a custom zeep.cache.Base implementation or disable caching entirely.

    To disable caching, pass googleads.common.ZeepServiceProxy.NO_CACHE to the cache parameter in the AdManagerClient initializer.

    import zeep
    
    # Configure a custom SQLite cache
    doc_cache = zeep.cache.SqliteCache(path=cache_path)
    ad_manager_client = ad_manager.AdManagerClient(
        oauth2_client, application_name, network_code=network_code, cache=doc_cache
    )
    
    # Disable caching entirely
    ad_manager_client = ad_manager.AdManagerClient(
        oauth2_client, application_name, network_code=network_code,
        cache=googleads.common.ZeepServiceProxy.NO_CACHE
    )