Wednesday, March 31, 2010

Generate Ad code using Java

// Copyright 2009 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v3;

import com.google.api.adsense.v3.AdSenseForContentService.GenerateAdCodeFault;
import com.google.api.adsense.v3.AdSenseForContentService.V3AdSenseForContentServicev3AdSenseForContentServiceSOAP11Port_httpsStub;

import org.apache.axis2.client.ServiceClient;
import org.apache.axiom.om.OMElement;

import java.util.Iterator;
import javax.xml.namespace.QName;

/**
* Sample code for calling AdSense API v3's AssociateAccount method.
*
* @author api.wes@gmail.com (Wes Goodman)
*/
public class GenerateAdCode {
static final String _DEVELOPER_EMAIL = "DEVELOPER_EMAIL";
static final String _DEVELOPER_PASS = "DEVELOPER_PASSWORD";
static final String _ADSENSE_V3_NS = "http://www.google.com/api/adsense/v3";

/**
* @param args
*/
public static void main(String[] args) {
try {
V3AdSenseForContentServicev3AdSenseForContentServiceSOAP11Port_httpsStub afc_service =
new V3AdSenseForContentServicev3AdSenseForContentServiceSOAP11Port_httpsStub();
ServiceClient service_client = afc_service._getServiceClient();
service_client.addStringHeader(new QName(_ADSENSE_V3_NS,
"developer_email"),
_DEVELOPER_EMAIL);
service_client.addStringHeader(new QName(_ADSENSE_V3_NS,
"developer_password"),
_DEVELOPER_PASS);
service_client.addStringHeader(new QName(_ADSENSE_V3_NS,
"client_id"),
"PUBLISHER CLIENT ID");

V3AdSenseForContentServicev3AdSenseForContentServiceSOAP11Port_httpsStub.GenerateAdCode generate_msg =
new V3AdSenseForContentServicev3AdSenseForContentServiceSOAP11Port_httpsStub.GenerateAdCode();
generate_msg.setSynServiceId("PUBLISHER SYNDICATION SERVICE ID");

V3AdSenseForContentServicev3AdSenseForContentServiceSOAP11Port_httpsStub.AdStyle ad_style =
new V3AdSenseForContentServicev3AdSenseForContentServiceSOAP11Port_httpsStub.AdStyle();
ad_style.setBackgroundColor("#000000");
ad_style.setBorderColor("#000000");
ad_style.setName("temp name");
ad_style.setTextColor("#FFFFFF");
ad_style.setTitleColor("FFFFFF");
ad_style.setUrlColor("#FFFFFF");
generate_msg.setAdStyle(ad_style);

V3AdSenseForContentServicev3AdSenseForContentServiceSOAP11Port_httpsStub.AdUnitType ad_unit_type =
new V3AdSenseForContentServicev3AdSenseForContentServiceSOAP11Port_httpsStub.AdUnitType();
ad_unit_type.setValue("TextOnly");
generate_msg.setAdUnitType(ad_unit_type);

V3AdSenseForContentServicev3AdSenseForContentServiceSOAP11Port_httpsStub.AdLayout ad_layout =
new V3AdSenseForContentServicev3AdSenseForContentServiceSOAP11Port_httpsStub.AdLayout();
ad_layout.setValue("728x90");
generate_msg.setAdLayout(ad_layout);

generate_msg.setAlternate(null);
generate_msg.setIsFramedPage(false);
generate_msg.setChannelName(null);

V3AdSenseForContentServicev3AdSenseForContentServiceSOAP11Port_httpsStub.CornerStyles corner_styles =
new V3AdSenseForContentServicev3AdSenseForContentServiceSOAP11Port_httpsStub.CornerStyles();
corner_styles.setValue("DEFAULT");
generate_msg.setCornerStyles(corner_styles);

generate_msg.setHostChannelNames(null);

V3AdSenseForContentServicev3AdSenseForContentServiceSOAP11Port_httpsStub.GenerateAdCodeResponse response =
afc_service.generateAdCode(generate_msg);

System.out.println("AdCode:");
System.out.println(response.get_return());

} catch (GenerateAdCodeFault fault) {
System.out.println("Due to axis issues, only AxisFault will ever be thrown");
System.out.println("Therefore, this catch block is for completeness only.");
} catch (org.apache.axis2.AxisFault fault) {
OMElement faultElement = fault.getDetail();
OMElement exElement = (OMElement) (faultElement.getChildElements().next());
Iterator i = exElement.getChildElements();
OMElement codeElement = (OMElement) i.next();
OMElement internalElement = (OMElement) i.next();
OMElement messageElement = (OMElement) i.next();
OMElement triggerElement = (OMElement) i.next();
OMElement triggerDetailsElement = (OMElement) i.next();
System.out.println("Code: " + codeElement.getText());
System.out.println("Internal: " + internalElement.getText());
System.out.println("Message: " + messageElement.getText());
System.out.println("Trigger: " + triggerElement.getText());
System.out.println("Trigger Detail: " + triggerDetailsElement.getText());
} catch (java.rmi.RemoteException e) {
System.out.println("remote exception");
}
}
}
\

Assosciate Account using Java

// Copyright 2009 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v3;

import com.google.api.adsense.v3.AccountService.AssociateAccountFault;
import com.google.api.adsense.v3.AccountService.V3AccountServicev3AccountServiceSOAP11Port_httpsStub;
import com.google.api.adsense.v3.AccountService.V3AccountServicev3AccountServiceSOAP11Port_httpsStub.SyndicationService_Data;

import org.apache.axis2.client.ServiceClient;
import org.apache.axiom.om.OMElement;

import java.util.Iterator;
import javax.xml.namespace.QName;

/**
* Sample code for calling AdSense API v3's AssociateAccount method.
*
* @author api.wes@gmail.com (Wes Goodman)
*/
public class AssociateAccount {
static final String _DEVELOPER_EMAIL = "DEVELOPER_EMAIL";
static final String _DEVELOPER_PASS = "DEVELOPER_PASSWORD";
static final String _ADSENSE_V3_NS = "http://www.google.com/api/adsense/v3";

/**
* @param args
*/
public static void main(String[] args) {
try {
V3AccountServicev3AccountServiceSOAP11Port_httpsStub account_service =
new V3AccountServicev3AccountServiceSOAP11Port_httpsStub();
ServiceClient service_client = account_service._getServiceClient();
service_client.addStringHeader(new QName(_ADSENSE_V3_NS,
"developer_email"),
_DEVELOPER_EMAIL);
service_client.addStringHeader(new QName(_ADSENSE_V3_NS,
"developer_password"),
_DEVELOPER_PASS);

V3AccountServicev3AccountServiceSOAP11Port_httpsStub.AssociateAccount associate_msg =
new V3AccountServicev3AccountServiceSOAP11Port_httpsStub.AssociateAccount();
associate_msg.setLoginEmail("USER_EMAIL");
// Either-or for PostalCode and Phone, though the service will work if both are set.
associate_msg.setPostalCode("USER_POSTAL_CODE");
associate_msg.setPhone("USER_PHONE_NUMBER_LAST_FIVE_DIGITS");
associate_msg.setDeveloperUrl("DEVELOPER_URL");

V3AccountServicev3AccountServiceSOAP11Port_httpsStub.AssociateAccountResponse response =
account_service.associateAccount(associate_msg);

V3AccountServicev3AccountServiceSOAP11Port_httpsStub.SyndicationService_Data[] synservice_response =
response.get_return();
for (SyndicationService_Data syndicationService_Data : synservice_response) {
System.out.println("Syndication Type: " + syndicationService_Data.getType().getValue());
System.out.println("Publisher Id: " + syndicationService_Data.getId());
}
} catch (AssociateAccountFault fault) {
System.out.println("Due to axis issues, only AxisFault will ever be thrown");
System.out.println("Therefore, this catch block is for completeness only.");
} catch (org.apache.axis2.AxisFault fault) {
OMElement faultElement = fault.getDetail();
OMElement exElement = (OMElement) (faultElement.getChildElements().next());
Iterator i = exElement.getChildElements();
OMElement codeElement = (OMElement) i.next();
OMElement internalElement = (OMElement) i.next();
OMElement messageElement = (OMElement) i.next();
OMElement triggerElement = (OMElement) i.next();
OMElement triggerDetailsElement = (OMElement) i.next();
System.out.println("Code: " + codeElement.getText());
System.out.println("Internal: " + internalElement.getText());
System.out.println("Message: " + messageElement.getText());
System.out.println("Trigger: " + triggerElement.getText());
System.out.println("Trigger Detail: " + triggerDetailsElement.getText());
} catch (java.rmi.RemoteException e) {
System.out.println("remote exception");
}
}
}

Create Account using Java

// Copyright 2009 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v3;

import com.google.api.adsense.v3.AccountService.CreateAccountFault;
import com.google.api.adsense.v3.AccountService.V3AccountServicev3AccountServiceSOAP11Port_httpsStub;
import com.google.api.adsense.v3.AccountService.V3AccountServicev3AccountServiceSOAP11Port_httpsStub.SyndicationService_Data;

import org.apache.axis2.client.ServiceClient;
import org.apache.axiom.om.OMElement;

import java.util.Iterator;
import javax.xml.namespace.QName;

/**
* Sample code for calling AdSense API v3's CreateAccount method.
*
* @author api.wes@gmail.com (Wes Goodman)
*/
public class CreateAccount {
static final String _DEVELOPER_EMAIL = "DEVELOPER_EMAIL";
static final String _DEVELOPER_PASS = "DEVELOPER_PASSWORD";
static final String _ADSENSE_V3_NS = "http://www.google.com/api/adsense/v3";

/**
* @param args
*/
public static void main(String[] args) {
try {
V3AccountServicev3AccountServiceSOAP11Port_httpsStub account_service =
new V3AccountServicev3AccountServiceSOAP11Port_httpsStub();
ServiceClient service_client = account_service._getServiceClient();
service_client.addStringHeader(new QName(_ADSENSE_V3_NS,
"developer_email"),
_DEVELOPER_EMAIL);
service_client.addStringHeader(new QName(_ADSENSE_V3_NS,
"developer_password"),
_DEVELOPER_PASS);

V3AccountServicev3AccountServiceSOAP11Port_httpsStub.CreateAccount account_msg =
new V3AccountServicev3AccountServiceSOAP11Port_httpsStub.CreateAccount();
account_msg.setLoginEmail("USER_EMAIL");

V3AccountServicev3AccountServiceSOAP11Port_httpsStub.EntityType entity_type =
new V3AccountServicev3AccountServiceSOAP11Port_httpsStub.EntityType();
entity_type.setValue("Individual");
account_msg.setEntityType(entity_type);

account_msg.setWebsiteUrl("USERS_PRIMARY_WEBSITE_URL");
account_msg.setWebsiteLocale("en");
account_msg.setUsersPreferredLocale("en_US");
account_msg.setEmailPromotionsPreference(false);

V3AccountServicev3AccountServiceSOAP11Port_httpsStub.SyndicationService_Type synservice_type =
new V3AccountServicev3AccountServiceSOAP11Port_httpsStub.SyndicationService_Type();
synservice_type.setValue("ContentAds");
account_msg.addSynServiceTypes(synservice_type);

account_msg.setDeveloperUrl("DEVELOPER_URL");

V3AccountServicev3AccountServiceSOAP11Port_httpsStub.CreateAccountResponse response =
account_service.createAccount(account_msg);

V3AccountServicev3AccountServiceSOAP11Port_httpsStub.SyndicationService_Data[] synservice_response =
response.get_return();
for (SyndicationService_Data syndicationService_Data : synservice_response) {
System.out.println("Syndication Type: " + syndicationService_Data.getType().getValue());
System.out.println("Publisher Id: " + syndicationService_Data.getId());
}
} catch (CreateAccountFault fault) {
System.out.println("Due to axis issues, only AxisFault will ever be thrown");
System.out.println("Therefore, this catch block is for completeness only.");
} catch (org.apache.axis2.AxisFault fault) {
OMElement faultElement = fault.getDetail();
OMElement exElement = (OMElement) (faultElement.getChildElements().next());
Iterator i = exElement.getChildElements();
OMElement codeElement = (OMElement) i.next();
OMElement internalElement = (OMElement) i.next();
OMElement messageElement = (OMElement) i.next();
OMElement triggerElement = (OMElement) i.next();
OMElement triggerDetailsElement = (OMElement) i.next();
System.out.println("Code: " + codeElement.getText());
System.out.println("Internal: " + internalElement.getText());
System.out.println("Message: " + messageElement.getText());
System.out.println("Trigger: " + triggerElement.getText());
System.out.println("Trigger Detail: " + triggerDetailsElement.getText());
} catch (java.rmi.RemoteException e) {
System.out.println("remote exception");
}
}
}