Google Affiliate Network API (Deprecated)

Getting Links and Banner Data

To get links and banner data, you must have an active Google Affiliate Network account.

Using the API

The Google Affiliate Network API allows publishers to request a list of links, information about a specific link, and create new publisher-specific links (when enabled by publishers). For more information, refer to the Links Reference.

Operations

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

Operation Description REST HTTP mappings
list Retrieves all links that match the query parameters. GET https://www.googleapis.com/gan/v1beta1/role/roleId/links
get Retrieves all available information about a single link. GET https://www.googleapis.com/gan/v1beta1/role/roleId/link/linkId
insert Creates a new publisher-specific link. POST https://www.googleapis.com/gan/v1beta1/role/roleId/link

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 links as a publisher, send an authenticated GET request to the following URL:

https://www.googleapis.com/gan/v1beta1/publishers/PUBLISHER_ID/links

To retrieve data for a specific links as an publisher, send an authenticated GET request to the following URL:

https://www.googleapis.com/gan/v1beta1/publishers/PUBLISHER_ID/link/linkId

To create a publisher-speicfic link as a publisher, send an authenticated POST request to the following URL:

https://www.googleapis.com/gan/v1beta1/publishers/PUBLISHER_ID/link

{
 "kind": "gan#link",
 "advertiserId": "ADVERTISER_ID",
 "name": "link_name",
 "destinationUrl": "destination_url"
}

Upon success, the server responds with an HTTP 200 OK status code and the publisher's links. For more information, refer to the Links 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')

To retrieve all links as a publisher:

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

# Retrieve links
list = links.list(**params).execute()

for link in list['items']:
  print link['name'] + ", " + link['clickTrackingUrl']

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

links = service.links()
params = {'role':'publishers', 'roleId':'PUBLISHER_ID', 'startDateMin':'2011-09-04T19:56:22.982Z'}

# Retrieve links.
list = links.list(**params).execute()

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

To retrieve a link by linkId as a publisher:

links = service.links()
params = {'role':'publishers', 'roleId':'PUBLISHER_ID', 'linkId':'link_id'}

# Retrieve link.
list = links.get(**params).execute()

print link

To create a publisher-specific link:

links = service.links()
insert_body = {
 'kind': 'gan#link',
 'advertiserId': str(ADVERTISER_ID), # Advertiser Id must be an integer.
 'name': 'link_name',
 'destinationUrl': 'destation_url'
 }

params = {'role':'publishers', 'roleId':'PUBLISHER_ID', 'body':'insert_body'}

# Create publisher-specific link.
list = links.insert(**params).execute()

For a list of all available parameters and fields returned, refer to the Links 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();
    ...
  }
...

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

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

//Execute request
List<Link> results = request.execute().getItems();
if (results == null) {
  System.out.println("No Results Returned!");
} else {
  for (Link e : results) {
    System.out.println("Link ID: " + e.getId() + " Link Name: " + e.getName());
  }
}

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

//Set up request
Gan.Links.List request = server.links().list("publishers", "publisherId");
request.setStartDateMin("2011-08-11T04:11:42.000Z");

//Execute request
List<Link> results = request.execute().getItems();
if (results == null) {
  System.out.println("No Results Returned!");
} else {
  for (Links e : results) {
    System.out.println("Link ID: " + e.getId() + " Link Name: " + e.getName());
  }
}

To retrieve a link by linkId as a publisher:

//Set up request
Long linkId = linkIdAsLong;

Link sLink = server.links().get("publishers", "publisherId", linkId).execute();

if (sLink == null) {
  System.out.println("Link Not Found!");
} else {
  System.out.println("Link Id:" + sLink.getId() + " Link Name:" + sLink.getName());
}

To create a publisher-specific link:

Link psl = new Link();
psl.setKind("gan#Link");
psl.setAdvertiserId(advertiserIdAsLong);
psl.setName("linkName");
psl.setDestinationUrl("destinationUrl");

Gan.Links.Insert insertRequest = server.links().insert("publishers", "publisherId", psl);
Link createdPsl = insertRequest.execute();
if (createdPsl == null) {
  System.out.println("Link Not Found!");
} else {
  System.out
      .println("Link Id: " + createdPsl.getId() + "Link Name:" + createdPsl.getName());
}

For a list of all available parameters and fields returned, refer to the Links 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.