Holistic Sample

The basics

We developed the AdSense Host API Holistic Sample to illustrate each step in the publisher signup flow, from starting an account association to generating ad code.

Signup is a 4-step process:

  1. The host requests a new association session by calling associationsessions.start.
  2. The host redirects the publisher to the AdSense website URL provided in the returned association session. The publisher then completes the form at that location.
  3. The publisher is redirected back to the host"s website with a token parameter included in the HTTP request.
  4. The host verifies the token by calling associationsessions.verify. If the association was created successfully, the response includes the publisher"s account ID, which the host should store locally for use with any subsequent API calls involving this publisher.

After retrieving the publisher"s account ID, the host needs to take two further steps:

  1. Create new ad units on the publisher account by calling accounts.adunits.insert.
  2. Generate the ad code for the new ad units by calling accounts.adunits.getAdCode.

Installing the holistic sample

  1. Start by downloading and installing the latest version of the Google APIs Client Library for Python. You can find some downloadable packages on the download section of the project page, but we recommend downloading and installing with pip.
  2. Download and install Django, making sure you get version 1.5.x (earlier or later versions may present compatibility issues). We recommend using pip for this as well.
  3. Download and install the Holistic Sample:
  4. git clone https://code.google.com/p/adsense-apis-showcase
  5. Modify samplehost/settings.py to add database information, setting the engine and name fields. Here are some sample settings for sqlite3:
  6. DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': '/path/to/your_db.db',
            'USER': '',
            'PASSWORD': '',
            'HOST': '',
            'PORT': '',
  7. In the same file, set TEMPLATE_DIRS to the absolute path of your template file directory. This is the templates/ subdirectory in your holistic sample installation.
  8. Once the Django project is fully configured, you need to initialize the database. From the root of your holistic sample installation, run the following command:
  9. python manage.py syncdb
  10. Next, add your developer credentials to client_secrets.json. You can find these in your APIs Console, once you"ve created a web application client ID.
  11. Finally, run the initial authentication against the API to create adsensehost.dat (which will store the credential data you get back from Google"s authorization service). Remember to use your AdSense Host account for this. From the root of your holistic sample installation, run the following command:
    python api_utils.py

The following video illustrates the entire sample installation process.

Running the holistic sample

  1. Start a local Django server to run the application; from the root of your holistic sample installation, run the following command:
    python manage.py runserver
  2. Make sure you copy the service URL. This is usually http://localhost:8000 (or, if using the IPv4 address), but may vary depending on your settings.
  3. Open the service URL in your browser and click on the only available blog. You"ll see a sample blog with no ads. You"re now acting as a publisher who owns a blog and is looking to monetize it.
  4. Open the administration page for that blog. You"ll see a "Monetize your blog with AdSense" link. Click it.
  5. You"ll be redirected to a signup page on AdSense where you can choose to create a new account or sign in with an existing one.
  6. Once you complete all the steps to sign up, you"ll be redirected to the callback URL on your holistic sample installation (http://localhost:8000/callback for the default settings).
  7. The sample validates the token received, creates an ad unit, generates the ad code for it, and redirects you to your blog, which now shows an ad at the top. If you go into the administration page, you"ll see the publisher ID for the newly-created (or associated) publisher account.

The following video illustrates how to run the holistic sample.