Last Updated: February 25, 2016
· themichael'tips

AdWords API in Java without Client Library


The tutorial Using the AdWords API in Java without the Client Library is outdated, here you can find tips to update and improve your client.

Tip 1: automate wsdl2java process

Once you have downloaded the wsdl file of your google service, add this code to your maven configuration file in order to extract and create the related java code of the service. This is done by wsdl2java binary.

 <!--  ..your stuff here.. -->

Tip 2: OAuth client sucks

Looking at the OAuth google's client, you can find within the package a bad code like this:

public class OfflineCredentials {

   * Enum representing the API that OfflineCredentials can be used for.
  public static enum Api {
    ADWORDS("api.adwords.", AdWordsInternals.getInstance()),
    DFA("api.dfa.", DfaInternals.getInstance()),
    DFP("api.dfp.", DfpInternals.getInstance());

    private final String propKeyPrefix;
    private final Internals internals;

    private Api(String propKeyPrefix, Internals internals) {
      this.propKeyPrefix = propKeyPrefix;
      this.internals = internals;

Expose internal class is an architectural error!

How define different services ? How can we extend one of the internal class to add/change some behaviour ? No way! You need to recompile the source code...etc...etc...

If you need to use different Google's api, not exclusively AdWords, for the authentication process you can cheat, and use one of the bellow services even if you don't directly use them.
A simple way to do authentication is:


Credential oAuth2Credential = new OfflineCredentials.Builder()

Within your src/main/resources/ file you have to use the "api.dfp" prefix naming for your credentials.

Tip 3: HTTP Header

Customize the HTTP header in order to pass the "Authorization: Bearer ACCESS_TOKEN":

Map<String, List<String>> extraHeader = new HashMap<String, List<String>>();
extraHeader.put("Authorization", Arrays.asList("Bearer " + ACCESS_TOKEN));
((BindingProvider)port).getRequestContext().put(Message.PROTOCOL_HEADERS, extraHeader);

Tip 4: request soap header

Your soap services may required a soap header, just take a look to your wsdl file.

/* create and init the soap header object */
GoogleSoapHeader soapHeader = new GoogleSoapHeader();

/* Here the technical stuff related to cxf library: */
GoogleObjectFactory obj = new GoogleObjectFactory();
JAXBElement<GoogleSoapHeader> authToken = obj.createGoogleRequestHeader(soapHeader);

List<Header> headersList = new ArrayList<Header>();
headersList.add(new Header(authToken.getName(), authToken.getValue(), 
new JAXBDataBinding(GoogleSoapHeader.class)));
((BindingProvider)port).getRequestContext().put(Header.HEADER_LIST, headersList);

Tip 5: Enjoy!

You can now call the service.