Python

Google provides a Python client library for interacting with the Ad Manager API. We recommend using the client library with PyPI.

To get started, create a new project in the IDE of your choice or add the dependency to an existing project. Google publishes client library artifacts to PyPI as google-ads-admanager .

 pip  
install  
google-ads-admanager 

Configure credentials

The Python client library uses OAuth2 and Application Default Credentials (ADC) to authenticate.

ADC searches for credentials in order in the following locations:

  1. GOOGLE_APPLICATION_CREDENTIALS environment variable.
  2. User credentials set up through the Google Cloud CLI (gcloud CLI).
  3. When running on Google Cloud, the service account attached to the Google Cloud resource.

For creating and configuring your ADC credentials, see Authentication .

Make your first request

Each service has a ServiceClient object with both synchronous and asynchronous methods for each REST method. The following example reads a Network synchronously.

  from 
  
 google.ads 
  
 import 
 admanager_v1 
 def 
  
 sample_get_network 
 (): 
 # Create a client 
 client 
 = 
 admanager_v1 
 . 
 NetworkServiceClient 
 () 
 # Initialize request argument(s) 
 request 
 = 
 admanager_v1 
 . 
 GetNetworkRequest 
 ( 
 name 
 = 
 "networks/[NETWORK_CODE]" 
 , 
 ) 
 # Make the request 
 response 
 = 
 client 
 . 
 get_network 
 ( 
 request 
 = 
 request 
 ) 
 # Handle the response 
 print 
 ( 
 response 
 ) 
  
 

For examples of other methods and resources, see the GitHub repository googleapis/google-cloud-python .

Log HTTP requests and responses

The Python client library library uses the standard Python logging library to log HTTP requests and responses. By default, logging is disabled.

To enable logging, set the environment variable GOOGLE_SDK_PYTHON_LOGGING_SCOPE . This environment variable configures handling of logging events at level logging.DEBUG or higher.

  # Log only Ad Manager API events 
 export 
  
 GOOGLE_SDK_PYTHON_LOGGING_SCOPE 
 = 
google.ads.admanager_v1 # Log all Google library events 
 export 
  
 GOOGLE_SDK_PYTHON_LOGGING_SCOPE 
 = 
google 

Alternatively, you can use the Python logging module:

  import 
  
 logging 
 from 
  
 google.ads 
  
 import 
 admanager_v1 
 logger 
 = 
 logging 
 . 
 getLogger 
 ( 
 "google.ads.admanager_v1" 
 ) 
 logger 
 . 
 addHandler 
 ( 
 logging 
 . 
 StreamHandler 
 ()) 
 logger 
 . 
 setLevel 
 ( 
 logging 
 . 
 DEBUG 
 ) 
 

Handle errors

All API errors extend the base class GoogleAPIError .

The error reason field uniquely identifies error types. Use this field to determine how to handle the error.

  try 
 : 
 network 
 = 
 client 
 . 
 get_network 
 ( 
 request 
 = 
 request 
 ) 
 print 
 ( 
 network 
 ) 
 except 
 GoogleAPIError 
 as 
 e 
 : 
 # Handle error 
 print 
 ( 
 e 
 . 
 reason 
 ) 
 

Ad Manager API errors also include a unique requestId you can provide to support for assistance with troubleshooting. The following example extracts the requestId from a GoogleAPIError ;

  except 
 GoogleAPIError 
 as 
 e 
 : 
 requestInfoType 
 = 
 "type.googleapis.com/google.rpc.RequestInfo" 
 requestInfo 
 = 
 [ 
 detail 
 for 
 detail 
 in 
 e 
 . 
 details 
 if 
 detail 
 [ 
 '@type' 
 ] 
 == 
 requestInfoType 
 ][ 
 0 
 ] 
 print 
 ( 
 requestInfo 
 [ 
 'requestId' 
 ]) 
 

Configure proxy settings

The Python client library respects environment variable settings http_proxy and https_proxy .

Design a Mobile Site
View Site in Mobile | Classic
Share by: