Blame | Last modification | View Log | RSS feed
AdWords API Java Client Library===============================The client library is distributed in two Maven artifacts. They are:<dependency><groupId>com.google.api-ads</groupId><artifactId>ads-lib</artifactId><version>RELEASE</version></dependency><dependency><groupId>com.google.api-ads</groupId><artifactId>adwords-axis</artifactId><version>RELEASE</version></dependency>The "ads-lib" artifact contains all of the library and utility classes foraccessing the AdWords API, but does not support any specific SOAP framework. Allclient library classes and utilities are in the package or sub-packages of"com.google.api.ads.adwords.lib". To add support for the Apache Axis framework,the "adwords-axis" plugin artifact is also necessary. This artifact alsoincludes autogenerated classes from the AdWords API. They are in the package"com.google.api.ads.adwords.axis.{version}".Quick Start Guide-----------------Before you begin, make sure retrieve your client ID and secret fromhttps://code.google.com/apis/console#accessExamples can be compiled and run by Maven by executing the following on thecommand line:$ cd path/to/extracted/distribution$ cd src/main/resourcesModify file with settings. This can be moved (not copied) to your homedirectory as well.$ vim ads.propertiesGenerate OAuth2 credentials to be used with other examples$ cd ../../../$ mvn -X compile$ mvn -X exec:java -Dexec.mainClass="adwords.axis.auth.GetRefreshTokenCopy the refresh token into your ads.properties file as you just didwith your general settings and client ID/secret.$ vim src/main/resources/ads.propertiesYou must have Maven recompile every time you modify the source code or resourcesin order for your changes to take effect.$ mvn -X compile$ mvn -X exec:java -Dexec.mainClass="adwords.axis.v201302.basicoperations.GetCampaigns"What's in the client library?-----------------------------The client library provides full access to all the functionality of the AdWordsAPI web services plus more. It includes:- Generated code: Classes and service clients generated automatically fromthe WSDLs.- AdWordsSession class: The AdWordsSession class represents one session ofAdWords API use. It is used to request the services the API offers. It willkeep track of state information, such as authentication informationgenerated from the API.- AdWordsServices class: This factory class is responsible for creating APIservice clients. It requires an AdWordsSession and the service interfacefrom the autogenerated code.- ads.properties file: This file in the src/main/resources directory is asample that shows you how to specify your credentials for the client libraryto read in. To use the credentials in an ads.properties file, make anAdWordsSession using the following code:new AdWordsSession.Builder().fromFile().build();The library will search for an ads.properties file in the following placesin order: in the user's home directory, in the current directory, andfinally in the classpath. Files in the src/main/resources directory show upon classpath. Every time you modify a file in src/main/resources, you musthave Maven recompile the project to pick up the changes. If you copy itto your home directory, you will not need to recompile every time.- Examples: This distribution contains examples in the "src/main/java"directory. We encourage you to use code samples to get started writing yourown application. All the code samples are runnable out of the box, but youwill have to set your credentials in "resources/ads.properties". You mayalso have to substitute in actual values places that are marked with thestring "INSERT_{SOMETHING}_HERE".Basic usage-----------To write a program that accesses AdWords accounts with the client library, dothe following:1) Import the following packages or classes:// Contains the data classes and service classes.import com.google.api.ads.adwords.axis.v201306.*;import com.google.api.ads.adwords.lib.client.AdWordsSession;import com.google.api.ads.adwords.lib.axis.factory.AdWordsServices;2a) Create an AdWordsSession instance, loading credentials from the propertiesfile:// Get an OAuth2 credential.Credential credential = new OfflineCredentials.Builder().forApi(OfflineCredentials.Api.AdWords).fromFile().build().generateCredential();// Construct an AdWordsSession.AdWordsSession session = new AdWordsSession.Builder().fromFile().withOAuth2Credential(credential).build();2b) Alternatively, you can specify your credentials in the constructor:// Get an OAuth2 credential.Credential credential = new OfflineCredentials.Builder().forApi(OfflineCredentials.Api.AdWords).withClientSecrets(clientId, clientSecret).withRefreshToken(refreshToken).build().generateCredential();// Construct an AdWordsSession.AdWordsSession session = new AdWordsSession.Builder().withDeveloperToken(developerToken)// ....withOAuth2Credential(credential).build();3) Instantiate the desired service class using the AdWordsServices utility anda Class object representing your service.// Get the CampaignService.CampaignServiceInterface campaignService =new AdWordsServices().get(session, CampaignServiceInterface.class);4) Create data objects and invoke methods on the service class instance. Thedata objects and methods map directly to the data objects and requests forthe corresponding web service.// Create selector.Selector selector = new Selector();selector.setFields(new String[] {"Id", "Name"});// Get all campaigns.CampaignPage page = campaignService.get(selector);You'll need to pull in the library as dependencies. This example projectcontains a full pom.xml that will pull in all of the necessary artifacts. Thereis no need to worry about accessing the WSDLs for the web services; the classesin the client library do it for you. Examples in the "src/main/java" directorycan be used to get started writing your own client.OAuth2 authentication---------------------There are two ways to authenticate with OAuth2 in the library:- Use the OfflineCredentials utility to generate a simple refreshableCredential for small applications. You will find that all of our examplesuse this utility.- Create a Credential object from scratch using a GoogleAuthorizationCodeFlowand a CredentialStore. You may want to go this route if you are creatinga complicated application. See the AdvancedCreateCredentialFromScratchexample for more information.How do I use OfflineCredentials?--------------------------------OfflineCredentials can use your ads.properties file to create a refreshableCredential. It will not create a refresh token for you, or walk the userthrough an authorization flow. To get a refresh token, run the GetRefreshTokenexample as shown in the Quick Start Guide section.Service accounts----------------You may be tempted to use a service account to access the API. However, to usea service account, you must be a Google Apps for Business customer, with theability to impersonate users of your network.If you are a Google Apps for Business customer, then you can use theAdvancedCreateCredentialFromScratch example as a starting point for constructinga service account Credential. Seehttps://code.google.com/p/google-api-java-client/wiki/OAuth2#Service_Accountson how to do this.If you are unable to use service accounts, but still want to only perform theOAuth2 flow once, consider creating an offline credential using theOfflineCredentials utility.How do I enable logging?------------------------The client library uses SLF4J for all logging. If you want to turn logging on,you must include a plugin that bridges SLF4J with a concrete logging framework.To quickly get you started and to serve as an example of how to do this, thisexample distribution uses the log4j framework.There are the following loggers within the library:- com.google.api.ads.adwords.lib.client.AdWordsServiceClient.soapXmlLoggerLogs incoming and outgoing SOAP requests/responses. SOAP requests andresponses are logged as WARN for exceptions and INFO for all other requests.You can configure your logging framework to accept logs on these parameters.See the example resources/log4j.properties for more information.- com.google.api.ads.adwords.lib.client.AdWordsServiceClient.requestInfoLoggerLogs all requests from the client library along with information such as thetimestamp, service, method, endpoint URL.To bridge SLF4J for a logging framework, you must do the following:1) Include the plugin and logging framework dependencies in the pom.xml file'sdependencies list.<!-- Adds the log4j framework --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.16</version></dependency><!-- Make SLF4J use log4j as the logging framework --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.6.2</version></dependency>2) If your logging framework requires a configuration file, you must place itin the resources directory. This distribution includes an exampleconfiguration for log4j named "log4j.properties".Because the client library uses SLF4J, the behavior of these loggers is highlycustomizable. Please see the "resources/log4j.properties" file for details onthe default behavior in this example project.What if I am behind an HTTP Proxy server?----------------------------------------It is recommended that the user set JVM arguments to configure this applicationfor their proxy.https.proxyHost Hostname of proxy server web-proxyhttps.proxyPort Port on server of proxy 8080https.proxyUser Optional username for proxy authentication someonehttps.proxyPassword Optional proxy server password secretThese properties can be set within the command line such as:$ mvn -Dhttps.proxyHost=web-proxy -Dhttps.proxyPort=8080-Dhttps.proxyUser=someone -Dhttps.proxyPassword=secret ...If necessary, set this up in code by doing the following:System.setProperty("https.proxyHost", "web-proxy");System.setProperty("https.proxyPort", "8080");System.setProperty("https.proxyUser", "someone");System.setProperty("https.proxyPassword", "secret");Where do I submit bug reports, feature requests and patches?------------------------------------------------------------All of these items can be submitted athttp://code.google.com/p/google-api-ads-java/issues/list.Make sure to subscribe to our Google Plus page for API change announcements andother news:https://plus.google.com/+GoogleAdsDevelopersAuthors:Adam RogalJoseph DiLalloMaintainer:Kevin Winter