### Install Google Ads Library from Tarball Source: https://github.com/googleads/googleads-python-lib/blob/main/README.md Alternatively, download the library as a tarball and install it using the setup.py script. Ensure setuptools is installed first. ```bash $ python setup.py build install ``` -------------------------------- ### Install Google Ads Library with Pip Source: https://github.com/googleads/googleads-python-lib/blob/main/README.md Install the googleads library and its dependencies from PyPI using pip. This is the recommended installation method. ```bash $ pip install googleads ``` -------------------------------- ### Example Output of generate_refresh_token.py Source: https://github.com/googleads/googleads-python-lib/wiki/API-access-using-own-credentials-(installed-application-flow) This is an example of the output you will see after approving the token and entering the verification code. It displays access and refresh tokens. ```text After approving the token enter the verification code (if specified). Code: **** Access token: **** Refresh token: **** ``` -------------------------------- ### Configure zeep cache with SqliteCache Source: https://github.com/googleads/googleads-python-lib/blob/main/README.md Pass an implementation of zeep.cache.Base to the AdManagerClient initializer to modify default caching behavior. This example configures a different location and duration for the cache file. ```python 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) ``` -------------------------------- ### Fetch Token and Get Credentials Source: https://github.com/googleads/googleads-python-lib/wiki/API-access-on-behalf-of-your-clients-(web-flow) Retrieve an access and refresh token by fetching the token using the authorization code received in the response. ```python flow.fetch_token(code=auth_code) credentials = flow.credentials ``` -------------------------------- ### Configure Logging for SOAP Interactions Source: https://github.com/googleads/googleads-python-lib/blob/main/README.md Configure Python's built-in logging framework to capture SOAP interactions. This example logs to stdout with a specific format and sets the googleads.soap logger to DEBUG level. ```python logging.basicConfig(level=logging.INFO, format=googleads.util.LOGGER_FORMAT) logging.getLogger('googleads.soap').setLevel(logging.DEBUG) ``` -------------------------------- ### Load Ad Manager Client from Storage (Custom Path) Source: https://github.com/googleads/googleads-python-lib/blob/main/README.md Initialize an Ad Manager client by loading credentials and settings from a specified googleads.yaml file. Provide the full path to the configuration file. ```python # Alternatively, pass in the location of the file: ad_manager_client = ad_manager.AdManagerClient.LoadFromStorage('C:\My\Directory\googleads.yaml') ``` -------------------------------- ### Load Ad Manager Client from Storage (Default Path) Source: https://github.com/googleads/googleads-python-lib/blob/main/README.md Initialize an Ad Manager client by loading credentials and settings from the default googleads.yaml file located in your home directory. ```python # Use the default location - your home directory: ad_manager_client = ad_manager.AdManagerClient.LoadFromStorage() ``` -------------------------------- ### Initialize Ad Manager Client with Service Account Credentials Source: https://github.com/googleads/googleads-python-lib/wiki/API-access-using-own-credentials-(server-to-server-flow) Initialize a GoogleServiceAccountClient using a JSON key file and API scope, then use this client to initialize an AdManagerClient. Ensure you have the key_file and application_name variables defined. ```python from googleads import ad_manager from googleads import oauth2 # Initialize the GoogleRefreshTokenClient using the credentials you received # in the earlier steps. oauth2_client = oauth2.GoogleServiceAccountClient( key_file, oauth2.GetAPIScope('ad_manager')) # Initialize the Ad Manager client. ad_manager_client = ad_manager.AdManagerClient(oauth2_client, application_name) ``` -------------------------------- ### Initialize Ad Manager Client from Storage Source: https://github.com/googleads/googleads-python-lib/wiki/API-access-using-own-credentials-(installed-application-flow) Initialize the Ad Manager client using the LoadFromStorage class method. This method reads credentials from the googleads.yaml file. ```python from googleads import ad_manager # Initialize the Ad Manager client. ad_manager_client = ad_manager.AdManagerClient.LoadFromStorage() ``` -------------------------------- ### Initialize Ad Manager Client Source: https://github.com/googleads/googleads-python-lib/wiki/API-access-on-behalf-of-your-clients-(web-flow) Initialize the Ad Manager client using a GoogleRefreshTokenClient with stored credentials. Provide client ID, secret, and refresh token. ```python from googleads import ad_manager from googleads import oauth2 # Initialize the GoogleRefreshTokenClient using the credentials you received # in the earlier steps. Oauth2_client = oauth2.GoogleRefreshTokenClient( client_id, client_secret, refresh_token) # Initialize the Ad Manager client. ad_manager_client = ad_manager.AdManagerClient( oauth2_client, application_name) ``` -------------------------------- ### Run generate_refresh_token.py with Command-Line Arguments Source: https://github.com/googleads/googleads-python-lib/wiki/API-access-using-own-credentials-(installed-application-flow) Execute the script to generate a refresh token by providing client ID and secret as arguments. Ensure these are replaced with your actual credentials. ```bash $ python generate_refresh_token.py --client_id INSERT_CLIENT_ID --client_secret INSERT_CLIENT_SECRET ``` -------------------------------- ### Initialize OAuth2 Flow Source: https://github.com/googleads/googleads-python-lib/wiki/API-access-on-behalf-of-your-clients-(web-flow) Initialize the Flow instance using client ID and secret for the OAuth2 web flow. Use GetAPIScope for Ad Manager scopes. ```python import google.oauth2.credentials import google_auth_oauthlib.flow # Initialize the flow using the client ID and secret downloaded earlier. # Note: You can use the GetAPIScope helper function to retrieve the # appropriate scope for Ad Manager. flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file( 'client_secret.json', scope=[oauth2.GetAPIScope('dfp')]) # Indicate where the API server will redirect the user after the user completes # the authorization flow. The redirect URI is required. flow.redirect_uri = 'https://www.example.com/oauth2callback' ``` -------------------------------- ### SOAP Envelope for Creating a Budget Source: https://github.com/googleads/googleads-python-lib/blob/main/tests/test_data/request_envelope_template.txt This XML snippet represents a SOAP envelope used to make a 'mutate' request to the Google Ads API to add a new budget. It includes necessary headers like clientCustomerId and developerToken, and specifies budget details such as name, amount, delivery method, and sharing settings. ```xml xxx-yyy-zzzz %s testing! false false ADD Test Budget #123456 50000000 STANDARD true ``` -------------------------------- ### Implement Custom Zeep Logger Plugin Source: https://github.com/googleads/googleads-python-lib/blob/main/README.md Create a custom Zeep plugin to log incoming and outgoing SOAP messages, including sensitive data that is normally stripped by default filters. Append this plugin to the client's zeep_client plugins. ```python 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()) ``` -------------------------------- ### Configure googleads.yaml for Ad Manager Client Source: https://github.com/googleads/googleads-python-lib/wiki/API-access-using-own-credentials-(installed-application-flow) Store your OAuth2 client ID, client secret, and refresh token in the googleads.yaml file under the 'ad_manager' section. This configuration is used to authenticate API requests. ```yaml ad_manager: # ... omitted ... client_id: INSERT_OAUTH2_CLIENT_ID_HERE client_secret: INSERT_OAUTH2_CLIENT_SECRET_HERE refresh_token: INSERT_REFRESH_TOKEN_HERE # ... omitted ... ``` -------------------------------- ### Generate Authorization URL Source: https://github.com/googleads/googleads-python-lib/wiki/API-access-on-behalf-of-your-clients-(web-flow) Generate the URL for Google's OAuth2 server to redirect the user. Enable offline access and incremental authorization. ```python # Generate URL for request to Google's OAuth 2.0 server. # Use kwargs to set optional request parameters. authorization_url, state = flow.authorization_url( # Enable offline access so that you can refresh an access token without # re-prompting the user for permission. Recommended for web server apps. access_type='offline', # Enable incremental authorization. Recommended as a best practice. include_granted_scopes='true', # Optional, set prompt to 'consent' will prompt the user for consent prompt='consent') ``` -------------------------------- ### Disable zeep caching Source: https://github.com/googleads/googleads-python-lib/blob/main/README.md Caching can be disabled by passing googleads.common.ZeepServiceProxy.NO_CACHE to the AdManagerClient initializer. ```python ad_manager_client = ad_manager.AdManagerClient( oauth2_client, application_name, network_code=network_code, cache=googleads.common.ZeepServiceProxy.NO_CACHE) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.