Install client libraries

The AdMob 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 calls that require user authorization. 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.

A Google APIs Client Generator can generate client libraries for Java, PHP, Python, Dart, and other languages. Here are the steps to generate the Java client library:

  1. Download the AdMob API definition into a temporary directory".
    export ADMOB_LIB_DIR=/tmp/admob_client
    mkdir -p ${ADMOB_LIB_DIR}
    curl -X GET \
        "https://admob.googleapis.com/\$discovery/rest?version=v1" \
        >${ADMOB_LIB_DIR}/admob_api_v1.json
  2. Install the generate_library tool as described in the "Google APIs Client Generator page on GitHub". It requires Python 2.7 and pip.
    pip install google-apis-client-generator
  3. Generate the library.
    generate_library \
        --input=${ADMOB_LIB_DIR}/admob_api_v1.json \
        --language=java \
        --output_dir=${ADMOB_LIB_DIR}/java
  4. Build the generated java library by an Apache Maven project.
    cd ${ADMOB_LIB_DIR}/java
    mvn clean install
  5. Include the library into your project. And use it to get a network report. The example pulls a network report from July 1 to July 5, 2019, showing clicks, revenue per impression, and estimated earnings by an app.
      public static void main(String[] args) {
        ...
        // Check the following docummentation to use OAuth 2.0 in your project.
        // 1. Java Google API Client Libraries;
        // 2. Using OAuth 2.0 with the Google API Client Library for Java pages.
        AdMob adMobClient = createAdMobApiRestClient(...);
        String publisherId = "pub-1234567890123456";
        GenerateNetworkReportRequest networkReportRequest = new GenerateNetworkReportRequest()
          .setReportSpec(
            new NetworkReportSpec()
              .setDateRange(
                new DateRange()
                  .setStartDate(new Date().setYear(2019).setMonth(7).setDay(1))
                  .setEndDate(new Date().setYear(2019).setMonth(7).setDay(5)))
              .setDimensions(Arrays.asList("DATE", "APP"))
              .setMetrics(Arrays.asList("ESTIMATED_EARNINGS", "IMPRESSION_RPM", "CLICKS")));
    
        // Send request to generate network report.
        InputStream networkReport = adMobClient
          .accounts()
          .networkReport()
          .generate("accounts/" + publisherId, request)
          .executeAsInputStream();
    
        ...
      }
    
      // Initialize AdMob client.
      public AdMob createAdMobApiRestClient(HttpTransport httpTransport, JsonFactory jsonFactory, Credential credential) {
        AdMob adMobClient = new AdMob.Builder(
          httpTransport,
          jsonFactory,
          request -> {
            credential.initialize(request);
            request.setParser(new JsonObjectParser(jsonFactory));
          })
          .setRootUrl("https://admob.googleapis.com")
          .build();
      }