// Get AdWordsUser from "~/adwords.properties".
AdWordsUser user = new AdWordsUser();
// Get the validation CampaignService.
CampaignServiceInterface campaignValidationService =
user.getValidationService(AdWordsService.V201306.CAMPAIGN_SERVICE);
// Create campaign.
Campaign goodCampaign = new Campaign();
goodCampaign.setName("Campaign #" + System.currentTimeMillis());
goodCampaign.setStatus(CampaignStatus.PAUSED);
BiddingStrategyConfiguration biddingConfig = new BiddingStrategyConfiguration();
ManualCpcBiddingScheme manualCpc = new ManualCpcBiddingScheme();
manualCpc.setEnhancedCpcEnabled(false);
biddingConfig.setBiddingScheme(manualCpc);
goodCampaign.setBiddingStrategyConfiguration(biddingConfig);
// Create budget.
Budget budget = new Budget();
budget.setPeriod(BudgetBudgetPeriod.DAILY);
budget.setAmount(new Money(null, 50000000L));
budget.setDeliveryMethod(BudgetBudgetDeliveryMethod.STANDARD);
goodCampaign.setBudget(budget);
KeywordMatchSetting keywordMatch = new KeywordMatchSetting();
keywordMatch.setOptIn(Boolean.TRUE);
goodCampaign.setSettings(new Setting[] {keywordMatch});
// Create operations.
CampaignOperation operation = new CampaignOperation();
operation.setOperand(goodCampaign);
operation.setOperator(Operator.ADD);
CampaignOperation[] operations = new CampaignOperation[] {operation};
// Validate campaign add operation.
CampaignReturnValue result = campaignValidationService.mutate(operations);
// Display new campaigns, which should be none if the service was a
// validation service.
if (result != null) {
for (Campaign campaignResult : result.getValue()) {
System.out.println("New campaign with name \"" + campaignResult.getName() + "\" and id \""
+ campaignResult.getId() + "\" was created.");
}
} else {
System.out.println("No campaigns created.");
}
// Provide an invalid bidding strategy that will cause an exception
// during validation.
Campaign badCampaign = new Campaign();
badCampaign.setName("Campaign #" + System.currentTimeMillis());
badCampaign.setStatus(CampaignStatus.PAUSED);
badCampaign.setBudget(budget);
// Throws RequiredError.REQUIRED @ operations[0].operand.biddingStrategyConfiguration.
badCampaign.setBiddingStrategyConfiguration(null);
// Create operations.
operation = new CampaignOperation();
operation.setOperand(badCampaign);
operation.setOperator(Operator.ADD);
operations = new CampaignOperation[] {operation};
try {
// Validate campaign add operation.
result = campaignValidationService.mutate(operations);
} catch (ApiException e) {
System.err.println("Validation failed for reason \"" + e.getMessage1() + "\".");
}
} catch (Exception e) {
e.printStackTrace();