use of com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation in project googleads-java-lib by googleads.
the class ValidateTextAd method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param adGroupId the ID of the ad group for the ad.
* @throws ApiException if the API request failed with one or more service errors.
* @throws RemoteException if the API request failed due to other errors.
*/
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session, Long adGroupId) throws RemoteException {
// Enable validation.
session.setValidateOnly(true);
// Get the validation AdGroupAdService.
AdGroupAdServiceInterface adGroupAdValidationService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
// Create text ad.
ExpandedTextAd textAd1 = new ExpandedTextAd();
textAd1.setHeadlinePart1("Luxury Cruise to Mars");
textAd1.setHeadlinePart2("Visit the Red Planet in style.");
textAd1.setDescription("Low-gravity fun for everyone!");
textAd1.setFinalUrls(new String[] { "http://www.example.com" });
// Create ad group ad.
AdGroupAd textAdGroupAd1 = new AdGroupAd();
textAdGroupAd1.setAdGroupId(adGroupId);
textAdGroupAd1.setAd(textAd1);
// Create operations.
AdGroupAdOperation textAdGroupAdOperation1 = new AdGroupAdOperation();
textAdGroupAdOperation1.setOperand(textAdGroupAd1);
textAdGroupAdOperation1.setOperator(Operator.ADD);
AdGroupAdOperation[] operations = new AdGroupAdOperation[] { textAdGroupAdOperation1 };
// Add ads.
adGroupAdValidationService.mutate(operations);
// No error means the request is valid.
// Now let's check an invalid ad using a very long line to trigger an error.
textAd1.setDescription("Low-gravity fun for all astronauts in orbit.");
try {
adGroupAdValidationService.mutate(operations);
} catch (ApiException e) {
System.err.printf("Validation failed for the following reason: %s%n", e.getMessage());
}
}
Aggregations