The Campaign Manager 360 API provides programmatic access to information from your Campaign Manager 360 account. It's used to manage and create campaigns and reports, just as you would through the Campaign Manager 360 and Report Builder web services.
This guide describes how to get started with the Campaign Manager 360 API.
Prerequisites
Before using the Campaign Manager 360 API, there are a few prerequisite steps you'll need to satisfy:
You must have a Campaign Manager 360 account. See Advertisers/Agencies for signup information.
Your Campaign Manager 360 account must be enabled for API Access. Most accounts have this enabled by default; if you're not sure, contact your account representative or the Campaign Manager 360 support team for assistance.
You must have a user profile with access to this account. Have your Campaign Manager 360 account administrator create a user profile associated with this account.
Check user profile permissions in the Campaign Manager 360 UI. These control what the user profile can access from the API. There are no separate API permissions.
Create a project
To get started using the Campaign Manager 360 API, you need to first create or select a project in the Google API Console and enable the API. Using this link guides you through the process and activates the Campaign Manager 360 API automatically.
Generate credentials
All requests you make to the Campaign Manager 360 API must be authorized. For a brief overview of authorization, read about how to authorize and identify your application to Google.
The following instructions guide you through the process of creating an OAuth 2.0 client ID to use with the installed application flow. For instructions on generating credentials for use with the service account flow, refer to the Service Accounts guide.
Follow the steps to configure a Google API Console project.
- Open the Credentials page in the API Console.
Click CREATE CREDENTIALS > OAuth client ID.
If you hadn't configured an OAuth consent screen for this project previously, you'll be directed to do so now. Click CONFIGURE CONSENT SCREEN.
Select the user type and click CREATE.
Fill out the initial form. You can edit this later if needed. Click Save when done.
Navigate back to Credentials > CREATE CREDENTIALS > OAuth client ID to continue.
Select Desktop app as the application type, give it a name, then click Create.
When done, you'll be presented with an OAuth 2.0 client ID and client secret, which you can download in JSON format and save for later use.
Install a client library
The Campaign Manager 360 API is built on HTTP and JSON, so any standard HTTP client can send requests to it and parse the responses.
However, the Google API client libraries provide better language integration, improved security, and support for making authorized requests. The client libraries are available in a number of programming languages; by using them you can avoid the need to manually set up HTTP requests and parse the responses.
To get started, select the programming language that you're using for development.
C#
Install the latest Campaign Manager 360 API client library for .NET. Using NuGet to manage your installation is recommended.
Open the NuGet Package Manager Console and run the following command:
Install-Package Google.Apis.Dfareporting.v3_4
Learn More
Java
Install the latest Campaign Manager 360 API client library for Java. Using Maven to manage your installation is recommended.
Add the following dependency to your pom.xml
file:
<dependency> <groupId>com.google.apis</groupId> <artifactId>google-api-services-dfareporting</artifactId> <version>v4-rev20220611-1.32.1</version> <exclusions> <exclusion> <groupId>com.google.guava</groupId> <artifactId>guava-jdk5</artifactId> </exclusion> </exclusions> </dependency>
Learn More
PHP
Install the latest Campaign Manager 360 API client library for PHP. Using Composer to manage your installation is recommended.
Open a terminal and run the following command:
composer require google/apiclient
If you've already installed the library and just want to update to the latest version:
composer update google/apiclient
Depending on your system, you may need to prepend these commands with
sudo
.
Learn More
Python
Install the latest Campaign Manager 360 API client library for Python. Using pip to manage your installation is recommended.
Open a terminal and run the following command:
pip install --upgrade google-api-python-client
Depending on your system, you may need to prepend these commands with
sudo
.
Learn More
Ruby
Install the latest Campaign Manager 360 API client library for Ruby. Using RubyGems to manage your installation is recommended.
Open a terminal and run the following command:
gem install google-api-client
If you've already installed the library and just want to update to the latest version:
gem update -y google-api-client
Depending on your system, you may need to prepend these commands with
sudo
.
Learn More
More supported languages can be found on the Client Libraries page.
Make a request
With OAuth 2.0 credentials created and a client library installed, you're ready to start using the Campaign Manager 360 API. Learn how to authorize, configure your client, and make your first request by following the quickstart below.
C#
Load the client secrets file and generate authorization credentials.
The first time you perform this step you'll be asked to accept an authorization prompt in your browser. Before accepting, make sure you're logged in with a Google Account that has access to Campaign Manager 360. Your application will be authorized to access data on behalf of whichever account is currently logged in.
// Load client secrets from the specified JSON file. GoogleClientSecrets clientSecrets; using(Stream json = new FileStream(pathToJsonFile, FileMode.Open, FileAccess.Read)) { clientSecrets = GoogleClientSecrets.Load(json); } // Create an asynchronous authorization task. // // Note: providing a data store allows auth credentials to be cached, so they survive multiple // runs of the application. This avoids prompting the user for authorization every time the // access token expires, by remembering the refresh token. The "user" value is used to // identify a specific set of credentials within the data store. You may provide different // values here to persist credentials for multiple users to the same data store. Task<UserCredential> authorizationTask = GoogleWebAuthorizationBroker.AuthorizeAsync( clientSecrets.Secrets, OAuthScopes, "user", CancellationToken.None, dataStore); // Authorize and persist credentials to the data store. UserCredential credential = authorizationTask.Result;
Create an authorized Dfareporting client.
// Create a Dfareporting service object. // // Note: application name should be replaced with a value that identifies your application. service = new DfareportingService( new BaseClientService.Initializer { HttpClientInitializer = credential, ApplicationName = "C# installed app sample" } );
Perform an operation.
// Retrieve and print all user profiles for the current authorized user. UserProfileList profiles = service.UserProfiles.List().Execute(); foreach (UserProfile profile in profiles.Items) { Console.WriteLine("Found user profile with ID {0} and name \"{1}\".", profile.ProfileId, profile.UserName); }
Java
Load the client secrets file and generate authorization credentials.
The first time you perform this step you'll be asked to accept an authorization prompt in your browser. Before accepting, make sure you're logged in with a Google Account that has access to Campaign Manager 360. Your application will be authorized to access data on behalf of whichever account is currently logged in.
// Load the client secrets JSON file. GoogleClientSecrets clientSecrets = GoogleClientSecrets.load( jsonFactory, Files.newBufferedReader(Paths.get(pathToClientSecretsFile), UTF_8)); // Set up the authorization code flow. // // Note: providing a DataStoreFactory allows auth credentials to be cached, so they survive // multiple runs of the program. This avoids prompting the user for authorization every time the // access token expires, by remembering the refresh token. GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( httpTransport, jsonFactory, clientSecrets, OAUTH_SCOPES) .setDataStoreFactory(dataStoreFactory) .build(); // Authorize and persist credentials to the data store. // // Note: the "user" value below is used to identify a specific set of credentials in the data // store. You may provide different values here to persist credentials for multiple users to // the same data store. Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
Create an authorized Dfareporting client.
// Create a Dfareporting client instance. // // Note: application name below should be replaced with a value that identifies your // application. Suggested format is "MyCompany-ProductName/Version.MinorVersion". Dfareporting reporting = new Dfareporting.Builder(credential.getTransport(), credential.getJsonFactory(), credential) .setApplicationName("dfareporting-java-installed-app-sample") .build();
Perform an operation.
// Retrieve and print all user profiles for the current authorized user. UserProfileList profiles = reporting.userProfiles().list().execute(); for (int i = 0; i < profiles.getItems().size(); i++) { System.out.printf("%d) %s%n", i + 1, profiles.getItems().get(i).getUserName()); }
PHP
Load the client secrets file and generate authorization credentials.
The first time you perform this step you'll be asked to accept an authorization prompt in your browser. Before accepting, make sure you're logged in with a Google Account that has access to Campaign Manager 360. Your application will be authorized to access data on behalf of whichever account is currently logged in.
// Create a Google_Client instance. // // Note: application name should be replaced with a value that identifies // your application. Suggested format is "MyCompany-ProductName". $client = new Google_Client(); $client->setAccessType('offline'); $client->setApplicationName('PHP installed app sample'); $client->setRedirectUri(self::OAUTH_REDIRECT_URI); $client->setScopes(self::$OAUTH_SCOPES); // Load the client secrets file. $client->setAuthConfig($pathToJsonFile); // Try to load cached credentials from the token store. Using a token store // allows auth credentials to be cached, so they survive multiple runs of // the application. This avoids prompting the user for authorization every // time the access token expires, by remembering the refresh token. if (file_exists($tokenStore) && filesize($tokenStore) > 0) { $client->setAccessToken(file_get_contents($tokenStore)); } else { // If no cached credentials were found, authorize and persist // credentials to the token store. print 'Open this URL in your browser and authorize the application.'; printf("\n\n%s\n\n", $client->createAuthUrl()); print 'Enter the authorization code: '; $code = trim(fgets(STDIN)); $client->authenticate($code); file_put_contents($tokenStore, json_encode($client->getAccessToken())); }
Create an authorized Dfareporting client.
// Create a Dfareporting service object. $service = new Google_Service_Dfareporting($client);
Perform an operation.
// Retrieve and print all user profiles for the current authorized user. $result = $service->userProfiles->listUserProfiles(); foreach ($result['items'] as $userProfile) { printf( "User profile \"%s\" (ID: %d) found for account %d.\n", $userProfile->getUserName(), $userProfile->getProfileId(), $userProfile->getAccountId() ); }
Python
Load the client secrets file and generate authorization credentials.
The first time you perform this step you'll be asked to accept an authorization prompt in your browser. Before accepting, make sure you're logged in with a Google Account that has access to Campaign Manager 360. Your application will be authorized to access data on behalf of whichever account is currently logged in.
# Set up a Flow object to be used if we need to authenticate. flow = client.flow_from_clientsecrets( path_to_client_secrets_file, scope=OAUTH_SCOPES) # Check whether credentials exist in the credential store. Using a credential # store allows auth credentials to be cached, so they survive multiple runs # of the application. This avoids prompting the user for authorization every # time the access token expires, by remembering the refresh token. storage = Storage(CREDENTIAL_STORE_FILE) credentials = storage.get() # If no credentials were found, go through the authorization process and # persist credentials to the credential store. if credentials is None or credentials.invalid: credentials = tools.run_flow(flow, storage, tools.argparser.parse_known_args()[0]) # Use the credentials to authorize an httplib2.Http instance. http = credentials.authorize(httplib2.Http())
Create an authorized Dfareporting client.
# Construct a service object via the discovery service. service = discovery.build('dfareporting', 'v4', http=http)
Perform an operation.
# Construct the request. request = service.userProfiles().list() # Execute request and print response. response = request.execute() for profile in response['items']: print('Found user profile with ID %s and user name "%s".' % (profile['profileId'], profile['userName']))
Ruby
Load the client secrets file and generate authorization credentials.
The first time you perform this step you'll be asked to accept an authorization prompt in your browser. Before accepting, make sure you're logged in with a Google Account that has access to Campaign Manager 360. Your application will be authorized to access data on behalf of whichever account is currently logged in.
# Load client ID from the specified file. client_id = Google::Auth::ClientId.from_file(path_to_json_file) # Set up the user authorizer. # # Note: providing a token store allows auth credentials to be cached, so they # survive multiple runs of the application. This avoids prompting the user for # authorization every time the access token expires, by remembering the # refresh token. authorizer = Google::Auth::UserAuthorizer.new( client_id, [API_NAMESPACE::AUTH_DFAREPORTING], token_store ) # Authorize and persist credentials to the data store. # # Note: the 'user' value below is used to identify a specific set of # credentials in the token store. You may provide different values here to # persist credentials for multiple users to the same token store. authorization = authorizer.get_credentials('user') if authorization.nil? puts format( "Open this URL in your browser and authorize the application.\n\n%s" \ "\n\nEnter the authorization code:", authorizer.get_authorization_url(base_url: OAUTH_REDIRECT_URI) ) code = STDIN.gets.chomp authorization = authorizer.get_and_store_credentials_from_code( base_url: OAUTH_REDIRECT_URI, code: code, user_id: 'user' ) end
Create an authorized Dfareporting client.
# Create a Dfareporting service object. # # Note: application name should be replaced with a value that identifies # your application. Suggested format is "MyCompany-ProductName". service = API_NAMESPACE::DfareportingService.new service.authorization = authorization service.client_options.application_name = 'Ruby installed app sample' service.client_options.application_version = '1.0.0'
Perform an operation.
// Retrieve and print all user profiles for the current authorized user. UserProfileList profiles = service.UserProfiles.List().Execute(); foreach (UserProfile profile in profiles.Items) { Console.WriteLine("Found user profile with ID {0} and name \"{1}\".", profile.ProfileId, profile.UserName); }
Learn more
Visit the API Reference to learn about all of the services the API has to offer. Every method detail page has an embedded API Explorer you can use to make test requests directly from your browser.
Review our other guides which cover advanced topics and provide end-to-end examples for common tasks.
When you're ready to start writing code, feel free to explore our extensive collection of code samples, which may be modified and extended to fit your needs.