Search in sources :

Example 16 with AdGroupAdOperation

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());
    }
}
Also used : AdGroupAd(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd) AdGroupAdServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface) ExpandedTextAd(com.google.api.ads.adwords.axis.v201809.cm.ExpandedTextAd) AdGroupAdOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation) ApiException(com.google.api.ads.adwords.axis.v201809.cm.ApiException)

Aggregations

AdGroupAd (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd)16 AdGroupAdOperation (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation)16 AdGroupAdServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface)15 AdGroupAdReturnValue (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdReturnValue)12 ExpandedTextAd (com.google.api.ads.adwords.axis.v201809.cm.ExpandedTextAd)5 ArrayList (java.util.ArrayList)5 ApiException (com.google.api.ads.adwords.axis.v201809.cm.ApiException)4 Parameter (com.beust.jcommander.Parameter)3 AdWordsServices (com.google.api.ads.adwords.axis.factory.AdWordsServices)3 AdGroupAdStatus (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdStatus)3 ApiError (com.google.api.ads.adwords.axis.v201809.cm.ApiError)3 Image (com.google.api.ads.adwords.axis.v201809.cm.Image)3 Operator (com.google.api.ads.adwords.axis.v201809.cm.Operator)3 AdWordsSession (com.google.api.ads.adwords.lib.client.AdWordsSession)3 AdWordsServicesInterface (com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface)3 ArgumentNames (com.google.api.ads.adwords.lib.utils.examples.ArgumentNames)3 OfflineCredentials (com.google.api.ads.common.lib.auth.OfflineCredentials)3 Api (com.google.api.ads.common.lib.auth.OfflineCredentials.Api)3 ConfigurationLoadException (com.google.api.ads.common.lib.conf.ConfigurationLoadException)3 OAuthException (com.google.api.ads.common.lib.exception.OAuthException)3