Google Affiliate Network API (Deprecated)

Getting Advertiser Information

To generate advertiser information, you must have an active Google Affiliate Network account.

Note: Using the API to directly serve live traffic is prohibited. Such usage will be rate limited or restricted by the API.

Generating HTML for Advertiser Lookup

Before you can use the script, you must have the following dependencies installed:

Then download the sample Python code for the Google Affiliate Network API. When you've downloaded the sample code, add your client information to the client_secret.json file. The client_secret.json file is where you will enter the OAuth/API key information found in the APIs Console.

The Python sample code for advertiser lookup is located in the advertisers.py file. It requires the advertisers_template.html file.

To generate a list of all advertisers that have a relationship with you as a publisher use the following command. Make sure to replace PUBLISHER_ID with your Publisher ID (such as k1234).

python advertisers.py --output_type=HTML publishers PUBLISHER_ID

To generate your own information as an advertiser use the following command. Make sure to replace ADVERTISER_ID with your Advertiser ID (such as k1234).

python advertisers.py --output_type=HTML --request_type=GET advertisers ADVERTISER_ID

To modify that HTML that is generated simply update the Django template advertisers_template.html.

Using the API

The Google Affiliate Network API allows publishers to request a list of advertisers and for advertisers to look up their own information. Certain parameters or fields are only available to certain roles used to access the API, such as publishers or advertisers. For more information, refer to the Advertisers Reference.

Operations

You can invoke the following methods on collections and resources in the Google Affiliate Network API:

Operation Description REST HTTP mappings
get Looks up and returns one resource. GET on a collection URI.
list Lists all resources within a collection. GET on a collection URI.

Calling Styles

There are two ways to invoke the Google Affiliate Network API:

  • Using REST directly to get back results in JSON.
  • Using client libraries for Python or Java.

REST

Authorizing Requests

Every request your application sends to the Google Affiliate Network API must include an authorization token. The token also identifies your application to Google.

About authorization protocols

We recommend using OAuth 2.0 to authorize requests.

If your application has certain unusual authorization requirements, such as logging in at the same time as requesting data access (hybrid) or domain-wide delegation of authority (2LO), then you cannot currently use OAuth 2.0 tokens. In such cases, you must instead use OAuth 1.0 tokens and an API key. You can find your application's API key in the Google APIs Console, in the Simple API Access section of the API Access pane.

Authorizing requests with OAuth 2.0

All requests to the Google Affiliate Network API must be authorized by an authenticated user.

The details of the authorization process, or "flow," for OAuth 2.0 vary somewhat depending on what kind of application you're writing. The following general process applies to all application types:

  1. When you create your application, you register it with Google. Google then provides information you'll need later, such as a client ID and a client secret.
  2. Activate the Google Affiliate Network API in the Services pane of the Google APIs Console. (If it isn't listed in the Console, then skip this step.)
  3. When your application needs access to user data, it asks Google for a particular scope of access.
  4. Google displays an OAuth dialog to the user, asking them to authorize your application to request some of their data.
  5. If the user approves, then Google gives your application a short-lived access token.
  6. Your application requests user data, attaching the access token to the request.
  7. If Google determines that your request and the token are valid, it returns the requested data.

Some flows include additional steps, such as using refresh tokens to acquire new access tokens. For detailed information about flows for various types of applications, see Google's OAuth 2.0 documentation.

Here's the OAuth 2.0 scope information for the Google Affiliate Network API:

Scope Meaning
https://www.googleapis.com/auth/gan read/write access to Google Affiliate Network API
https://www.googleapis.com/auth/gan.readonly read-only access to Google Affiliate Network API

To request access using OAuth 2.0, your application needs the scope information, as well as information that Google supplies during application registration (such as the client ID and/or the client secret).

Tip: The Google APIs client libraries can handle some of the authorization process for you. They are available for a variety of programming languages; check the Libraries and Samples page for more details.

Using REST

To retrieve all advertisers that you have a relationship with (approved, pending, declined, or deactivated), send an authenticated GET request to the following URL:

https://www.googleapis.com/gan/v1beta1/publishers/PUBLISHER_ID/advertisers?parameters

To retrieve your own information as an advertiser, send an authenticated GET request to the following URL:

https://www.googleapis.com/gan/v1beta1/advertisers/ADVERTISER_ID/advertisers

Upon success, the server responds with an HTTP 200 OK status code and the publisher's relationships. For more information, refer to the Advertisers Reference.

JSON Data format

JSON (JavaScript Object Notation) is a common, language-independent data format that provides a simple text representation of arbitrary data structures. For more information, see json.org.

Python

Setup

To start developing in Python, download and install the Google APIs Client Library for Python.

You can now import the classes you will need using the following statements:

from apiclient.discovery import build
from apiclient.oauth import OAuthCredentials

import httplib2
import oauth2 as oauth

Instantiating the client

  1. Visit the Google APIs Console
  2. Sign-in and create a project or use an existing project.
  3. In the Services pane , activate Google Affiliate Network API for your project. If prompted, read and accept the terms of service.
  4. Go to the API Access pane:
  5. Click Create an OAuth 2.0 client ID....
    1. Fill out the Branding Information fields and click Next.
    2. In Client ID Settings, set Application type to Installed application.
    3. Click Create client ID
  6. Later on you will need to know the:
    • Client ID
    • Client secret
    • Redirect URIs

The following code demonstrates how to configure your client and authenticate using OAuth 2.0 for native applications.

import gflags
import httplib2

from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run

FLAGS = gflags.FLAGS

# Set up a Flow object to be used if we need to authenticate. This
# sample uses OAuth 2.0, and we set up the OAuth2WebServerFlow with
# the information it needs to authenticate. Note that it is called
# the Web Server Flow, but it can also handle the flow for native
# applications">applications
# The client_id and client_secret are copied from the API Access tab on
# the Google APIs Console
FLOW = OAuth2WebServerFlow(
    client_id='YOUR_CLIENT_ID',
    client_secret='YOUR_CLIENT_SECRET',
    scope='https://www.googleapis.com/auth/gan',
    user_agent='YOUR_APPLICATION_NAME/YOUR_APPLICATION_VERSION')

# To disable the local server feature, uncomment the following line:
# FLAGS.auth_local_webserver = False

# If the Credentials don't exist or are invalid, run through the native client
# flow. The Storage object will ensure that if successful the good
# Credentials will get written back to a file.
storage = Storage('offers.dat')
credentials = storage.get()
if credentials is None or credentials.invalid == True:
  credentials = run(FLOW, storage)

# Create an httplib2.Http object to handle our HTTP requests and authorize it
# with our good Credentials.
http = httplib2.Http()
http = credentials.authorize(http)

# Build a service object for interacting with the API. Visit
# the Google APIs Console
# to get a developerKey for your own application.
service = build(serviceName='gan', version='v1beta1', http=http,
       developerKey='YOUR_API_KEY')

Retrieving all advertisers

To retrieve all advertisers that have a relationship with the requester (approved, pending, declined, or deactivated) as a publisher:

advertisers = service.advertisers()
params = {'role':'publishers', 'roleId':'PUBLISHER_ID'}

# Retrieve the relevant relationships.
list = advertisers.list(**params).execute()

for advertiser in list['items']:
  print advertiser['name'] + ", " + advertiser['id']

To filter based on a parameter such as relationshipStatus, simply add the parameter list():

advertisers = service.advertisers()
params = {'role':'publishers', 'roleId':'PUBLISHER_ID', 'relationshipStatus':'approved'}

# Retrieve the relevant relationships.
list = advertisers.list(**params).execute()

for advertiser in list['items']:
  print advertiser

For a list of all available parameters and fields returned, refer to the Advertisers Reference. To see a complete working example, refer to the Google Affiliate Network API samples.

Java

Setup

Using the Google APIs Client Library for Java requires that you download the core Java client library and the the Google Affiliate Network API Java library .jar files. Download the files and include them in your build path:

Core client library .jar files Lists the latest .jar files for the Google APIs Client Library for Java.
Google Affiliate Network API client library Provides instructions for how to use the Google Affiliate Network API.
Google Affiliate Network API samples Provides samples for the Google Affiliate Network API.

Instantiating the client

Every query made to the Google Affiliate Network API must be associated with a registered project in the Google APIs Console

Creating a project will provide you with a Client ID and Key that will be used to identify your application when making requests to the Google Affiliate Network API. To learn more about the Client IDs and Keys, read Keys, access, security, and identity.

  1. Visit the Google APIs Console
  2. Sign-in and create a project or use an existing project
  3. In the Services pane, activate Google Affiliate Network API for your project
  4. Click Create an OAuth 2.0 client ID....
    1. Fill out the Branding Information fields and click Next.
    2. In Client ID Settings, set Application type to Installed application.
    3. Click Create client ID
  5. In the next step, you will need to know the:
    • Client ID
    • Client secret
    • Redirect URIs

Each Java application needs to include it's Client ID and Client secret. The following Java examples use some helper classes (found in the Java Client Library) to simplify using these values. In this example, you must add these values to a file named client_secrets.json. This file must then be compiled and installed.

The following code demonstrates how to configure your client and authenticate using OAuth 2.0 for native applications once the client_secrets.json file has been updated.

import com.google.api.services.gan.*;
import com.google.api.services.gan.model.*;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.services.samples.shared.cmdline.oauth2.LocalServerReceiver;
import com.google.api.services.samples.shared.cmdline.oauth2.OAuth2Native;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.client.http.javanet.NetHttpTransport;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

...

  public void setUp() throws IOException {
    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();
    List scopes = Arrays.asList("https://www.googleapis.com/auth/gan");

    /**
     * Make sure to update the client_secrets.json file or you will get an error.
     * See the Java sample code for more information.
     */
    Credential credential = OAuth2Native.authorize(httpTransport,
        jsonFactory, new LocalServerReceiver(), scopes);

    server = new Gan.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
        .setApplicationName("Your_Application_Name/Version").build();
    ...
  }
...

Retrieving all advertisers

To retrieve all advertisers as a publisher (make sure to replace publisherId):

//Set up request
Gan.Advertisers.List request = server.advertisers().list("publishers", "publisherId");

//Execute request
List<Advertisers> results = request.execute().getItems();
if (results == null) {
  System.out.println("No Results Returned!");
} else {
  for (Advertiser a : results) {
    System.out.println(a);
  }
}

To filter based on a parameter such as the relationshipStatus, use the appropriate Setter:

//Set up request
Gan.Advertisers.List request = server.advertisers().list("publishers", "publisherId");
request.setRelationshipStatus("approved");

//Execute request
List<Advertisers> results = request.execute().getItems();
if (results == null) {
  System.out.println("No Results Returned!");
} else {
  for (Advertisers a : results) {
    System.out.println(a);
  }
}

For a list of all available parameters and fields returned, refer to the Advertisers Reference. To see a complete working example, refer to the Google Affiliate Network API samples.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.