// Copyright 2005 Google Inc. All rights reserved. import com.google.api.adsense.v2.*; import org.apache.axis.client.Stub; /** * Demo of using the Account service and AdSenseForContent service * to generate a javascript ad code snippet. *

* com.google.api.adsense.v2 is generated via WSDl2Java, like this:
*

 * java org.apache.axis.wsdl.WSDL2Java -p com.google.api.adsense.v2 \
 * https://www.google.com/api/adsense/v2/AccountService?wsdl
 * java org.apache.axis.wsdl.WSDL2Java -p com.google.api.adsense.v2 \
 * https://www.google.com/api/adsense/v2/AdSenseForContent?wsdl
 * 
*/ public class GenerateAdCodeSnippet { // SOAP Headers static final String developerEmail = "REPLACE WITH DEVELOPER'S EMAIL"; static final String developerPassword = "REPLACE WITH DEVELOPER'S PASSWORD"; static final String clientId = "REPLACE WITH CLIENT'S ID"; /* The namespace used for API headers. */ private static final String apiNS="http://www.google.com/api/adsense/v2"; /** * This simple demonstration program generates an ad code snippet. */ public static void main(String args[]) throws Exception { // Get Axis to give us a stub for the Account service AccountService accountService = (new AccountServiceServiceLocator()).getAccountService(); // Set the SOAP headers ((Stub) accountService).setHeader(apiNS, "developer_email", developerEmail); ((Stub) accountService).setHeader(apiNS, "developer_password", developerPassword); ((Stub) accountService).setHeader(apiNS, "client_id", clientId); // Get Axis to give us a stub for the AdSenseForContent service AdSenseForContentService adSenseForContentService = (new AdSenseForContentServiceServiceLocator()).getAdSenseForContentService(); // Set the SOAP headers ((Stub) adSenseForContentService).setHeader(apiNS, "developer_email", developerEmail); ((Stub) adSenseForContentService).setHeader(apiNS, "developer_password", developerPassword); ((Stub) adSenseForContentService).setHeader(apiNS, "client_id", clientId); // Get the user's AdSense for Content syndication service ID SyndicationService_Data synServiceData = accountService.getSyndicationService("ContentAds"); String synServiceId = synServiceData.getId(); /* * we need to create a style for the code we will generate; we could * use a style we previously saved with saveAdStyle, but here we assume we * don't have any such styles */ AdStyle adStyle = new AdStyle(); adStyle.setName("demoStyle"); adStyle.setBorderColor("#0000FF"); adStyle.setBackgroundColor("#FFFFFF"); adStyle.setTitleColor("#FF0000"); adStyle.setTextColor("#00FF00"); adStyle.setUrlColor("#FFFF00"); // create the other parameters to generateAdCode String adUnitType = "TextOnly"; String layout = "728x90"; String alternate = "#FFFFFF"; boolean isFramedPage = false; String channelName = null; String codeSnippet = adSenseForContentService.generateAdCode( synServiceId, adStyle, adUnitType, layout, alternate, isFramedPage, channelName); System.out.println("The code snippet to put on the page:"); System.out.println(codeSnippet); } }