Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
9005 manish.sha 1
AdWords API Java Client Library
2
===============================
3
 
4
The client library is distributed in two Maven artifacts. They are:
5
 
6
    <dependency>
7
      <groupId>com.google.api-ads</groupId>
8
      <artifactId>ads-lib</artifactId>
9
      <version>RELEASE</version>
10
    </dependency>
11
    <dependency>
12
      <groupId>com.google.api-ads</groupId>
13
      <artifactId>adwords-axis</artifactId>
14
      <version>RELEASE</version>
15
    </dependency>
16
 
17
The "ads-lib" artifact contains all of the library and utility classes for
18
accessing the AdWords API, but does not support any specific SOAP framework. All
19
client library classes and utilities are in the package or sub-packages of
20
"com.google.api.ads.adwords.lib". To add support for the Apache Axis framework,
21
the "adwords-axis" plugin artifact is also necessary. This artifact also
22
includes autogenerated classes from the AdWords API. They are in the package
23
"com.google.api.ads.adwords.axis.{version}".
24
 
25
 
26
Quick Start Guide
27
-----------------
28
 
29
Before you begin, make sure retrieve your client ID and secret from
30
https://code.google.com/apis/console#access
31
 
32
Examples can be compiled and run by Maven by executing the following on the
33
command line:
34
 
35
    $ cd path/to/extracted/distribution
36
    $ cd src/main/resources
37
 
38
    Modify file with settings. This can be moved (not copied) to your home
39
    directory as well.
40
 
41
    $ vim ads.properties
42
 
43
    Generate OAuth2 credentials to be used with other examples
44
 
45
    $ cd ../../../
46
    $ mvn -X compile
47
    $ mvn -X exec:java -Dexec.mainClass="adwords.axis.auth.GetRefreshToken
48
 
49
    Copy the refresh token into your ads.properties file as you just did
50
    with your general settings and client ID/secret.
51
 
52
    $ vim src/main/resources/ads.properties
53
 
54
    You must have Maven recompile every time you modify the source code or resources
55
    in order for your changes to take effect.
56
 
57
    $ mvn -X compile
58
    $ mvn -X exec:java -Dexec.mainClass="adwords.axis.v201302.basicoperations.GetCampaigns"
59
 
60
 
61
What's in the client library?
62
-----------------------------
63
 
64
The client library provides full access to all the functionality of the AdWords
65
API web services plus more. It includes:
66
 
67
  - Generated code: Classes and service clients generated automatically from
68
    the WSDLs.
69
 
70
  - AdWordsSession class: The AdWordsSession class represents one session of
71
    AdWords API use. It is used to request the services the API offers. It will
72
    keep track of state information, such as authentication information
73
    generated from the API.
74
 
75
  - AdWordsServices class: This factory class is responsible for creating API
76
    service clients. It requires an AdWordsSession and the service interface
77
    from the autogenerated code.
78
 
79
  - ads.properties file: This file in the src/main/resources directory is a
80
    sample that shows you how to specify your credentials for the client library
81
    to read in. To use the credentials in an ads.properties file, make an
82
    AdWordsSession using the following code:
83
 
84
    new AdWordsSession.Builder().fromFile().build();
85
 
86
    The library will search for an ads.properties file in the following places
87
    in order: in the user's home directory, in the current directory, and
88
    finally in the classpath. Files in the src/main/resources directory show up
89
    on classpath. Every time you modify a file in src/main/resources, you must
90
    have Maven recompile the project to pick up the changes. If you copy it
91
    to your home directory, you will not need to recompile every time.
92
 
93
  - Examples: This distribution contains examples in the "src/main/java"
94
    directory. We encourage you to use code samples to get started writing your
95
    own application. All the code samples are runnable out of the box, but you
96
    will have to set your credentials in "resources/ads.properties". You may
97
    also have to substitute in actual values places that are marked with the
98
    string "INSERT_{SOMETHING}_HERE".
99
 
100
 
101
Basic usage
102
-----------
103
 
104
To write a program that accesses AdWords accounts with the client library, do
105
the following:
106
 
107
  1) Import the following packages or classes:
108
 
109
     // Contains the data classes and service classes.
110
     import com.google.api.ads.adwords.axis.v201306.*;
111
 
112
     import com.google.api.ads.adwords.lib.client.AdWordsSession;
113
     import com.google.api.ads.adwords.lib.axis.factory.AdWordsServices;
114
 
115
  2a) Create an AdWordsSession instance, loading credentials from the properties
116
      file:
117
 
118
      // Get an OAuth2 credential.
119
      Credential credential = new OfflineCredentials.Builder()
120
          .forApi(OfflineCredentials.Api.AdWords)
121
          .fromFile()
122
          .build()
123
          .generateCredential();
124
 
125
      // Construct an AdWordsSession.
126
      AdWordsSession session = new AdWordsSession.Builder()
127
          .fromFile()
128
          .withOAuth2Credential(credential)
129
          .build();
130
 
131
  2b) Alternatively, you can specify your credentials in the constructor:
132
 
133
      // Get an OAuth2 credential.
134
      Credential credential = new OfflineCredentials.Builder()
135
          .forApi(OfflineCredentials.Api.AdWords)
136
          .withClientSecrets(clientId, clientSecret)
137
          .withRefreshToken(refreshToken)
138
          .build()
139
          .generateCredential();
140
 
141
      // Construct an AdWordsSession.
142
      AdWordsSession session = new AdWordsSession.Builder()
143
          .withDeveloperToken(developerToken)
144
          // ...
145
          .withOAuth2Credential(credential)
146
          .build();
147
 
148
  3) Instantiate the desired service class using the AdWordsServices utility and
149
     a Class object representing your service.
150
 
151
     // Get the CampaignService.
152
     CampaignServiceInterface campaignService =
153
         new AdWordsServices().get(session, CampaignServiceInterface.class);
154
 
155
  4) Create data objects and invoke methods on the service class instance. The
156
     data objects and methods map directly to the data objects and requests for
157
     the corresponding web service.
158
 
159
      // Create selector.
160
      Selector selector = new Selector();
161
      selector.setFields(new String[] {"Id", "Name"});
162
 
163
      // Get all campaigns.
164
      CampaignPage page = campaignService.get(selector);
165
 
166
You'll need to pull in the library as dependencies. This example project
167
contains a full pom.xml that will pull in all of the necessary artifacts. There
168
is no need to worry about accessing the WSDLs for the web services; the classes
169
in the client library do it for you. Examples in the "src/main/java" directory
170
can be used to get started writing your own client.
171
 
172
 
173
OAuth2 authentication
174
---------------------
175
 
176
There are two ways to authenticate with OAuth2 in the library:
177
 
178
  - Use the OfflineCredentials utility to generate a simple refreshable
179
    Credential for small applications. You will find that all of our examples
180
    use this utility.
181
 
182
  - Create a Credential object from scratch using a GoogleAuthorizationCodeFlow
183
    and a CredentialStore. You may want to go this route if you are creating
184
    a complicated application. See the AdvancedCreateCredentialFromScratch
185
    example for more information.
186
 
187
 
188
How do I use OfflineCredentials?
189
--------------------------------
190
 
191
OfflineCredentials can use your ads.properties file to create a refreshable
192
Credential. It will not create a refresh token for you, or walk the user
193
through an authorization flow. To get a refresh token, run the GetRefreshToken
194
example as shown in the Quick Start Guide section.
195
 
196
 
197
Service accounts
198
----------------
199
 
200
You may be tempted to use a service account to access the API. However, to use
201
a service account, you must be a Google Apps for Business customer, with the
202
ability to impersonate users of your network.
203
 
204
If you are a Google Apps for Business customer, then you can use the
205
AdvancedCreateCredentialFromScratch example as a starting point for constructing
206
a service account Credential. See
207
https://code.google.com/p/google-api-java-client/wiki/OAuth2#Service_Accounts
208
on how to do this.
209
 
210
If you are unable to use service accounts, but still want to only perform the
211
OAuth2 flow once, consider creating an offline credential using the
212
OfflineCredentials utility.
213
 
214
 
215
How do I enable logging?
216
------------------------
217
 
218
The client library uses SLF4J for all logging. If you want to turn logging on,
219
you must include a plugin that bridges SLF4J with a concrete logging framework.
220
To quickly get you started and to serve as an example of how to do this, this
221
example distribution uses the log4j framework.
222
 
223
There are the following loggers within the library:
224
 
225
  - com.google.api.ads.adwords.lib.client.AdWordsServiceClient.soapXmlLogger
226
 
227
    Logs incoming and outgoing SOAP requests/responses. SOAP requests and
228
    responses are logged as WARN for exceptions and INFO for all other requests.
229
    You can configure your logging framework to accept logs on these parameters.
230
    See the example resources/log4j.properties for more information.
231
 
232
  - com.google.api.ads.adwords.lib.client.AdWordsServiceClient.requestInfoLogger
233
 
234
    Logs all requests from the client library along with information such as the
235
    timestamp, service, method, endpoint URL.
236
 
237
To bridge SLF4J for a logging framework, you must do the following:
238
 
239
  1) Include the plugin and logging framework dependencies in the pom.xml file's
240
     dependencies list.
241
 
242
     <!-- Adds the log4j framework -->
243
     <dependency>
244
       <groupId>log4j</groupId>
245
       <artifactId>log4j</artifactId>
246
       <version>1.2.16</version>
247
     </dependency>
248
     <!-- Make SLF4J use log4j as the logging framework -->
249
     <dependency>
250
       <groupId>org.slf4j</groupId>
251
       <artifactId>slf4j-log4j12</artifactId>
252
       <version>1.6.2</version>
253
     </dependency>
254
 
255
  2) If your logging framework requires a configuration file, you must place it
256
     in the resources directory. This distribution includes an example
257
     configuration for log4j named "log4j.properties".
258
 
259
Because the client library uses SLF4J, the behavior of these loggers is highly
260
customizable. Please see the "resources/log4j.properties" file for details on
261
the default behavior in this example project.
262
 
263
 
264
What if I am behind an HTTP Proxy server?
265
----------------------------------------
266
 
267
It is recommended that the user set JVM arguments to configure this application
268
for their proxy.
269
 
270
    https.proxyHost      Hostname of proxy server                      web-proxy
271
    https.proxyPort      Port on server of proxy                       8080
272
    https.proxyUser      Optional username for proxy authentication    someone
273
    https.proxyPassword  Optional proxy server password                secret
274
 
275
These properties can be set within the command line such as:
276
 
277
    $ mvn -Dhttps.proxyHost=web-proxy -Dhttps.proxyPort=8080
278
        -Dhttps.proxyUser=someone -Dhttps.proxyPassword=secret ...
279
 
280
If necessary, set this up in code by doing the following:
281
 
282
    System.setProperty("https.proxyHost", "web-proxy");
283
    System.setProperty("https.proxyPort", "8080");
284
    System.setProperty("https.proxyUser", "someone");
285
    System.setProperty("https.proxyPassword", "secret");
286
 
287
 
288
Where do I submit bug reports, feature requests and patches?
289
------------------------------------------------------------
290
 
291
All of these items can be submitted at
292
http://code.google.com/p/google-api-ads-java/issues/list.
293
 
294
Make sure to subscribe to our Google Plus page for API change announcements and
295
other news:
296
 
297
  https://plus.google.com/+GoogleAdsDevelopers
298
 
299
Authors:
300
    Adam Rogal
301
    Joseph DiLallo
302
 
303
Maintainer:
304
    Kevin Winter