Search in sources :

Example 1 with Api

use of com.google.api.ads.common.lib.auth.OfflineCredentials.Api in project googleads-java-lib by googleads.

the class AddMultiAssetResponsiveDisplayAd method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param adGroupId the ID of the ad group where the ad will be created.
 * @throws ApiException if the API request failed with one or more service errors.
 * @throws RemoteException if the API request failed due to other errors.
 * @throws IOException if unable to retrieve an image from a URL.
 */
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session, long adGroupId) throws IOException {
    // Get the AdGroupAdService.
    AdGroupAdServiceInterface adGroupAdService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
    List<AdGroupAdOperation> operations = new ArrayList<>();
    MultiAssetResponsiveDisplayAd ad = new MultiAssetResponsiveDisplayAd();
    List<AssetLink> headlines = new ArrayList<>();
    headlines.add(createAssetLinkForText("Travel to Mars"));
    headlines.add(createAssetLinkForText("Travel to Jupiter"));
    headlines.add(createAssetLinkForText("Travel to Pluto"));
    headlines.add(createAssetLinkForText("Experience the Stars"));
    ad.setHeadlines(headlines.toArray(new AssetLink[0]));
    List<AssetLink> descriptions = new ArrayList<>();
    descriptions.add(createAssetLinkForText("Visit the planet in a luxury spaceship."));
    descriptions.add(createAssetLinkForText("See the planet in style."));
    ad.setDescriptions(descriptions.toArray(new AssetLink[0]));
    ad.setBusinessName("Galactic Luxury Cruises");
    ad.setLongHeadline(createAssetLinkForText("Visit the planet in a luxury spaceship."));
    // This ad format does not allow the creation of an image asset by setting the asset.imageData
    // field. An image asset must first be created using the AssetService, and asset.assetId must be
    // populated when creating the ad.
    ad.setMarketingImages(new AssetLink[] { createAssetLinkForImageAsset(uploadImageAsset(adWordsServices, session, "https://goo.gl/3b9Wfh")) });
    ad.setSquareMarketingImages(new AssetLink[] { createAssetLinkForImageAsset(uploadImageAsset(adWordsServices, session, "https://goo.gl/mtt54n")) });
    ad.setFinalUrls(new String[] { "http://www.example.com" });
    // Optional: set call to action text.
    ad.setCallToActionText("Shop Now");
    // Set color settings using hexadecimal values. Set allowFlexibleColor to false if you want
    // your ads to render by always using your colors strictly.
    ad.setMainColor("#0000ff");
    ad.setAccentColor("#ffff00");
    ad.setAllowFlexibleColor(false);
    // Set the format setting that the ad will be served in.
    ad.setFormatSetting(DisplayAdFormatSetting.NON_NATIVE);
    // Optional: Set dynamic display ad settings, composed of landscape logo image, promotion text,
    // and price prefix.
    ad.setDynamicSettingsPricePrefix("as low as");
    ad.setDynamicSettingsPromoText("Free shipping!");
    ad.setLogoImages(new AssetLink[] { createAssetLinkForImageAsset(uploadImageAsset(adWordsServices, session, "https://goo.gl/mtt54n")) });
    // Create ad group ad.
    AdGroupAd adGroupAd = new AdGroupAd();
    adGroupAd.setAdGroupId(adGroupId);
    adGroupAd.setAd(ad);
    // Optional: set the status.
    adGroupAd.setStatus(AdGroupAdStatus.PAUSED);
    // Create the operation.
    AdGroupAdOperation adGroupAdOperation = new AdGroupAdOperation();
    adGroupAdOperation.setOperand(adGroupAd);
    adGroupAdOperation.setOperator(Operator.ADD);
    operations.add(adGroupAdOperation);
    // Add ad.
    AdGroupAdReturnValue result = adGroupAdService.mutate(operations.toArray(new AdGroupAdOperation[operations.size()]));
    Arrays.stream(result.getValue()).map(adGroupAdResult -> (MultiAssetResponsiveDisplayAd) adGroupAdResult.getAd()).forEach(newAd -> System.out.printf("New responsive display ad with ID %d and long headline '%s' was added.%n", newAd.getId(), ((TextAsset) newAd.getLongHeadline().getAsset()).getAssetText()));
}
Also used : Arrays(java.util.Arrays) Parameter(com.beust.jcommander.Parameter) ArrayList(java.util.ArrayList) AssetLink(com.google.api.ads.adwords.axis.v201809.cm.AssetLink) AssetServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AssetServiceInterface) OfflineCredentials(com.google.api.ads.common.lib.auth.OfflineCredentials) ApiException(com.google.api.ads.adwords.axis.v201809.cm.ApiException) AssetOperation(com.google.api.ads.adwords.axis.v201809.cm.AssetOperation) ImageAsset(com.google.api.ads.adwords.axis.v201809.cm.ImageAsset) ArgumentNames(com.google.api.ads.adwords.lib.utils.examples.ArgumentNames) Credential(com.google.api.client.auth.oauth2.Credential) AdGroupAdReturnValue(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdReturnValue) AdGroupAdStatus(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdStatus) OAuthException(com.google.api.ads.common.lib.exception.OAuthException) DisplayAdFormatSetting(com.google.api.ads.adwords.axis.v201809.cm.DisplayAdFormatSetting) AdWordsServices(com.google.api.ads.adwords.axis.factory.AdWordsServices) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) IOException(java.io.IOException) Operator(com.google.api.ads.adwords.axis.v201809.cm.Operator) CodeSampleParams(com.google.api.ads.common.lib.utils.examples.CodeSampleParams) RemoteException(java.rmi.RemoteException) DEFAULT_CONFIGURATION_FILENAME(com.google.api.ads.common.lib.utils.Builder.DEFAULT_CONFIGURATION_FILENAME) AdGroupAd(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd) List(java.util.List) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) AdWordsServicesInterface(com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface) ApiError(com.google.api.ads.adwords.axis.v201809.cm.ApiError) ValidationException(com.google.api.ads.common.lib.exception.ValidationException) MultiAssetResponsiveDisplayAd(com.google.api.ads.adwords.axis.v201809.cm.MultiAssetResponsiveDisplayAd) AdGroupAdOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation) Api(com.google.api.ads.common.lib.auth.OfflineCredentials.Api) TextAsset(com.google.api.ads.adwords.axis.v201809.cm.TextAsset) AdGroupAdServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface) TextAsset(com.google.api.ads.adwords.axis.v201809.cm.TextAsset) AdGroupAd(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd) MultiAssetResponsiveDisplayAd(com.google.api.ads.adwords.axis.v201809.cm.MultiAssetResponsiveDisplayAd) AdGroupAdReturnValue(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdReturnValue) AdGroupAdServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface) ArrayList(java.util.ArrayList) AdGroupAdOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation) AssetLink(com.google.api.ads.adwords.axis.v201809.cm.AssetLink)

Example 2 with Api

use of com.google.api.ads.common.lib.auth.OfflineCredentials.Api in project googleads-java-lib by googleads.

the class AddExpandedTextAds method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param adGroupId the ID of the ad group where the ad will be created.
 * @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 {
    // Get the AdGroupAdService.
    AdGroupAdServiceInterface adGroupAdService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
    List<AdGroupAdOperation> operations = new ArrayList<>();
    for (int i = 0; i < NUMBER_OF_ADS; i++) {
        // Create expanded text ad.
        ExpandedTextAd expandedTextAd = new ExpandedTextAd();
        expandedTextAd.setHeadlinePart1(String.format("Cruise #%d to Mars", i));
        expandedTextAd.setHeadlinePart2("Best Space Cruise Line");
        expandedTextAd.setHeadlinePart3("For Your Loved Ones");
        expandedTextAd.setDescription("Buy your tickets now!");
        expandedTextAd.setDescription2("Discount ends soon");
        expandedTextAd.setFinalUrls(new String[] { "http://www.example.com/" + i });
        // Create ad group ad.
        AdGroupAd expandedTextAdGroupAd = new AdGroupAd();
        expandedTextAdGroupAd.setAdGroupId(adGroupId);
        expandedTextAdGroupAd.setAd(expandedTextAd);
        // Optional: set the status.
        expandedTextAdGroupAd.setStatus(AdGroupAdStatus.PAUSED);
        // Create the operation.
        AdGroupAdOperation adGroupAdOperation = new AdGroupAdOperation();
        adGroupAdOperation.setOperand(expandedTextAdGroupAd);
        adGroupAdOperation.setOperator(Operator.ADD);
        operations.add(adGroupAdOperation);
    }
    // Add ads.
    AdGroupAdReturnValue result = adGroupAdService.mutate(operations.toArray(new AdGroupAdOperation[operations.size()]));
    // Display ads.
    Arrays.stream(result.getValue()).map(adGroupAdResult -> (ExpandedTextAd) adGroupAdResult.getAd()).forEach(newAd -> System.out.printf("Expanded text ad with ID %d and headline '%s | %s%s' was added.%n", newAd.getId(), newAd.getHeadlinePart1(), newAd.getHeadlinePart2(), newAd.getHeadlinePart3() == null ? "" : String.format(" | %s", newAd.getHeadlinePart3())));
}
Also used : Arrays(java.util.Arrays) Parameter(com.beust.jcommander.Parameter) ArrayList(java.util.ArrayList) OfflineCredentials(com.google.api.ads.common.lib.auth.OfflineCredentials) ApiException(com.google.api.ads.adwords.axis.v201809.cm.ApiException) ArgumentNames(com.google.api.ads.adwords.lib.utils.examples.ArgumentNames) Credential(com.google.api.client.auth.oauth2.Credential) AdGroupAdReturnValue(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdReturnValue) AdGroupAdStatus(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdStatus) OAuthException(com.google.api.ads.common.lib.exception.OAuthException) AdWordsServices(com.google.api.ads.adwords.axis.factory.AdWordsServices) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) Operator(com.google.api.ads.adwords.axis.v201809.cm.Operator) CodeSampleParams(com.google.api.ads.common.lib.utils.examples.CodeSampleParams) ExpandedTextAd(com.google.api.ads.adwords.axis.v201809.cm.ExpandedTextAd) RemoteException(java.rmi.RemoteException) DEFAULT_CONFIGURATION_FILENAME(com.google.api.ads.common.lib.utils.Builder.DEFAULT_CONFIGURATION_FILENAME) AdGroupAd(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd) List(java.util.List) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) AdWordsServicesInterface(com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface) ApiError(com.google.api.ads.adwords.axis.v201809.cm.ApiError) ValidationException(com.google.api.ads.common.lib.exception.ValidationException) AdGroupAdOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation) Api(com.google.api.ads.common.lib.auth.OfflineCredentials.Api) AdGroupAdServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface) AdGroupAd(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd) AdGroupAdReturnValue(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdReturnValue) AdGroupAdServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface) ArrayList(java.util.ArrayList) ExpandedTextAd(com.google.api.ads.adwords.axis.v201809.cm.ExpandedTextAd) AdGroupAdOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation)

Example 3 with Api

use of com.google.api.ads.common.lib.auth.OfflineCredentials.Api in project googleads-java-lib by googleads.

the class HandlePartialFailures method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param adGroupId the ID of the ad group.
 * @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 partial failure.
    session.setPartialFailure(true);
    // Get the AdGroupCriterionService.
    AdGroupCriterionServiceInterface adGroupCriterionService = adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
    List<AdGroupCriterionOperation> operations = new ArrayList<>();
    // Create keywords.
    String[] keywords = new String[] { "mars cruise", "inv@lid cruise", "venus cruise", "b(a)d keyword cruise" };
    for (String keywordText : keywords) {
        // Create keyword
        Keyword keyword = new Keyword();
        keyword.setText(keywordText);
        keyword.setMatchType(KeywordMatchType.BROAD);
        // Create biddable ad group criterion.
        BiddableAdGroupCriterion keywordBiddableAdGroupCriterion = new BiddableAdGroupCriterion();
        keywordBiddableAdGroupCriterion.setAdGroupId(adGroupId);
        keywordBiddableAdGroupCriterion.setCriterion(keyword);
        // Create operation.
        AdGroupCriterionOperation keywordAdGroupCriterionOperation = new AdGroupCriterionOperation();
        keywordAdGroupCriterionOperation.setOperand(keywordBiddableAdGroupCriterion);
        keywordAdGroupCriterionOperation.setOperator(Operator.ADD);
        operations.add(keywordAdGroupCriterionOperation);
    }
    // Add ad group criteria.
    AdGroupCriterionReturnValue result = adGroupCriterionService.mutate(operations.toArray(new AdGroupCriterionOperation[] {}));
    // Display results.
    Arrays.stream(result.getValue()).filter(adGroupCriterionResult -> adGroupCriterionResult.getCriterion() != null).forEach(adGroupCriterionResult -> System.out.printf("Ad group criterion with ad group ID %d, and criterion ID %d, " + "and keyword '%s' was added.%n", adGroupCriterionResult.getAdGroupId(), adGroupCriterionResult.getCriterion().getId(), ((Keyword) adGroupCriterionResult.getCriterion()).getText()));
    for (ApiError apiError : result.getPartialFailureErrors()) {
        // Get the index of the failed operation from the error's field path elements.
        FieldPathElement[] fieldPathElements = apiError.getFieldPathElements();
        FieldPathElement firstFieldPathElement = null;
        if (fieldPathElements != null && fieldPathElements.length > 0) {
            firstFieldPathElement = fieldPathElements[0];
        }
        if (firstFieldPathElement != null && "operations".equals(firstFieldPathElement.getField()) && firstFieldPathElement.getIndex() != null) {
            int operationIndex = firstFieldPathElement.getIndex();
            AdGroupCriterion adGroupCriterion = operations.get(operationIndex).getOperand();
            System.out.printf("Ad group criterion with ad group ID %d and keyword '%s' " + "triggered a failure for the following reason: %s.%n", adGroupCriterion.getAdGroupId(), ((Keyword) adGroupCriterion.getCriterion()).getText(), apiError.getErrorString());
        } else {
            System.out.printf("A failure has occurred for the following reason: %s%n", apiError.getErrorString());
        }
    }
}
Also used : KeywordMatchType(com.google.api.ads.adwords.axis.v201809.cm.KeywordMatchType) Arrays(java.util.Arrays) BiddableAdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.BiddableAdGroupCriterion) Parameter(com.beust.jcommander.Parameter) AdGroupCriterionServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionServiceInterface) ArrayList(java.util.ArrayList) OfflineCredentials(com.google.api.ads.common.lib.auth.OfflineCredentials) ApiException(com.google.api.ads.adwords.axis.v201809.cm.ApiException) FieldPathElement(com.google.api.ads.adwords.axis.v201809.cm.FieldPathElement) ArgumentNames(com.google.api.ads.adwords.lib.utils.examples.ArgumentNames) Credential(com.google.api.client.auth.oauth2.Credential) OAuthException(com.google.api.ads.common.lib.exception.OAuthException) AdGroupCriterionReturnValue(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionReturnValue) AdWordsServices(com.google.api.ads.adwords.axis.factory.AdWordsServices) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) Keyword(com.google.api.ads.adwords.axis.v201809.cm.Keyword) Operator(com.google.api.ads.adwords.axis.v201809.cm.Operator) CodeSampleParams(com.google.api.ads.common.lib.utils.examples.CodeSampleParams) RemoteException(java.rmi.RemoteException) DEFAULT_CONFIGURATION_FILENAME(com.google.api.ads.common.lib.utils.Builder.DEFAULT_CONFIGURATION_FILENAME) List(java.util.List) AdGroupCriterionOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionOperation) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) AdWordsServicesInterface(com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface) AdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterion) ApiError(com.google.api.ads.adwords.axis.v201809.cm.ApiError) ValidationException(com.google.api.ads.common.lib.exception.ValidationException) Api(com.google.api.ads.common.lib.auth.OfflineCredentials.Api) AdGroupCriterionServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionServiceInterface) Keyword(com.google.api.ads.adwords.axis.v201809.cm.Keyword) ArrayList(java.util.ArrayList) FieldPathElement(com.google.api.ads.adwords.axis.v201809.cm.FieldPathElement) AdGroupCriterionOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionOperation) AdGroupCriterionReturnValue(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionReturnValue) BiddableAdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.BiddableAdGroupCriterion) BiddableAdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.BiddableAdGroupCriterion) AdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterion) ApiError(com.google.api.ads.adwords.axis.v201809.cm.ApiError)

Example 4 with Api

use of com.google.api.ads.common.lib.auth.OfflineCredentials.Api in project googleads-java-lib by googleads.

the class GetKeywords method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param adGroupId the ID of the ad group to use to find keywords.
 * @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 {
    // Get the AdGroupCriterionService.
    AdGroupCriterionServiceInterface adGroupCriterionService = adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
    int offset = 0;
    boolean morePages = true;
    // Create selector.
    SelectorBuilder builder = new SelectorBuilder();
    Selector selector = builder.fields(AdGroupCriterionField.Id, AdGroupCriterionField.CriteriaType, AdGroupCriterionField.KeywordMatchType, AdGroupCriterionField.KeywordText).orderAscBy(AdGroupCriterionField.KeywordText).offset(offset).limit(PAGE_SIZE).in(AdGroupCriterionField.AdGroupId, adGroupId.toString()).in(AdGroupCriterionField.CriteriaType, "KEYWORD").build();
    while (morePages) {
        // Get all ad group criteria.
        AdGroupCriterionPage page = adGroupCriterionService.get(selector);
        // Display ad group criteria.
        if (page.getEntries() != null && page.getEntries().length > 0) {
            // Display results.
            Arrays.stream(page.getEntries()).map(adGroupCriterionResult -> (Keyword) adGroupCriterionResult.getCriterion()).forEach(keyword -> System.out.printf("Keyword with text '%s', match type '%s', criteria type '%s'," + " and ID %d was found.%n", keyword.getText(), keyword.getMatchType(), keyword.getType(), keyword.getId()));
        } else {
            System.out.println("No ad group criteria were found.");
        }
        offset += PAGE_SIZE;
        selector = builder.increaseOffsetBy(PAGE_SIZE).build();
        morePages = offset < page.getTotalNumEntries();
    }
}
Also used : OAuthException(com.google.api.ads.common.lib.exception.OAuthException) Arrays(java.util.Arrays) Parameter(com.beust.jcommander.Parameter) AdWordsServices(com.google.api.ads.adwords.axis.factory.AdWordsServices) AdGroupCriterionPage(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionPage) AdGroupCriterionServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionServiceInterface) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) Keyword(com.google.api.ads.adwords.axis.v201809.cm.Keyword) CodeSampleParams(com.google.api.ads.common.lib.utils.examples.CodeSampleParams) SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) RemoteException(java.rmi.RemoteException) DEFAULT_CONFIGURATION_FILENAME(com.google.api.ads.common.lib.utils.Builder.DEFAULT_CONFIGURATION_FILENAME) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) AdWordsServicesInterface(com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface) ApiError(com.google.api.ads.adwords.axis.v201809.cm.ApiError) ValidationException(com.google.api.ads.common.lib.exception.ValidationException) OfflineCredentials(com.google.api.ads.common.lib.auth.OfflineCredentials) ApiException(com.google.api.ads.adwords.axis.v201809.cm.ApiException) Api(com.google.api.ads.common.lib.auth.OfflineCredentials.Api) Selector(com.google.api.ads.adwords.axis.v201809.cm.Selector) ArgumentNames(com.google.api.ads.adwords.lib.utils.examples.ArgumentNames) Credential(com.google.api.client.auth.oauth2.Credential) AdGroupCriterionField(com.google.api.ads.adwords.lib.selectorfields.v201809.cm.AdGroupCriterionField) AdGroupCriterionServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionServiceInterface) SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) Keyword(com.google.api.ads.adwords.axis.v201809.cm.Keyword) AdGroupCriterionPage(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionPage) Selector(com.google.api.ads.adwords.axis.v201809.cm.Selector)

Example 5 with Api

use of com.google.api.ads.common.lib.auth.OfflineCredentials.Api in project googleads-java-lib by googleads.

the class AddResponsiveDisplayAd method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param adGroupId the ID of the ad group where the ad will be created.
 * @throws ApiException if the API request failed with one or more service errors.
 * @throws RemoteException if the API request failed due to other errors.
 * @throws IOException if uploading an image failed.
 */
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session, long adGroupId) throws IOException {
    // Get the MediaService.
    MediaServiceInterface mediaService = adWordsServices.get(session, MediaServiceInterface.class);
    // Get the AdGroupAdService.
    AdGroupAdServiceInterface adGroupAdService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
    // Create a responsive display ad.
    ResponsiveDisplayAd responsiveDisplayAd = new ResponsiveDisplayAd();
    // This ad format does not allow the creation of an image using the
    // Image.data field. An image must first be created using the MediaService,
    // and Image.mediaId must be populated when creating the ad.
    long marketingImageMediaId = uploadImage(mediaService, "https://goo.gl/3b9Wfh");
    Image marketingImage = new Image();
    marketingImage.setMediaId(marketingImageMediaId);
    responsiveDisplayAd.setMarketingImage(marketingImage);
    responsiveDisplayAd.setShortHeadline("Travel");
    responsiveDisplayAd.setLongHeadline("Travel the World");
    responsiveDisplayAd.setDescription("Take to the air!");
    responsiveDisplayAd.setBusinessName("Interplanetary Cruises");
    responsiveDisplayAd.setFinalUrls(new String[] { "http://www.example.com/" });
    // Optional: Create a square marketing image using MediaService, and set it
    // to the ad.
    long squareMarketingImageMediaId = uploadImage(mediaService, "https://goo.gl/mtt54n");
    Image squareMarketingImage = new Image();
    squareMarketingImage.setMediaId(squareMarketingImageMediaId);
    responsiveDisplayAd.setSquareMarketingImage(squareMarketingImage);
    // Optional: set call to action text.
    responsiveDisplayAd.setCallToActionText("Shop Now");
    // Optional: Set dynamic display ad settings, composed of landscape logo
    // image, promotion text, and price prefix.
    DynamicSettings dynamicDisplayAdSettings = createDynamicDisplayAdSettings(mediaService);
    responsiveDisplayAd.setDynamicDisplayAdSettings(dynamicDisplayAdSettings);
    // Whitelisted accounts only: Set color settings using hexadecimal values.
    // Set allowFlexibleColor to false if you want your ads to render by always
    // using your colors strictly.
    /*
    responsiveDisplayAd.setMainColor("#0000ff");
    responsiveDisplayAd.setAccentColor("#ffff00");
    responsiveDisplayAd.setAllowFlexibleColor(false);
    */
    // Whitelisted accounts only: Set the format setting that the ad will be
    // served in.
    /*
    responsiveDisplayAd.setFormatSetting(
        com.google.api.ads.adwords.axis.v201809.cm.DisplayAdFormatSetting.NON_NATIVE);
    */
    // Create ad group ad for the responsive display ad.
    AdGroupAd adGroupAd = new AdGroupAd();
    adGroupAd.setAdGroupId(adGroupId);
    adGroupAd.setAd(responsiveDisplayAd);
    // Optional: set the status.
    adGroupAd.setStatus(AdGroupAdStatus.PAUSED);
    // Create the operation.
    AdGroupAdOperation adGroupAdOperation = new AdGroupAdOperation();
    adGroupAdOperation.setOperand(adGroupAd);
    adGroupAdOperation.setOperator(Operator.ADD);
    // Make the mutate request.
    AdGroupAdReturnValue result = adGroupAdService.mutate(new AdGroupAdOperation[] { adGroupAdOperation });
    // Display ads.
    Arrays.stream(result.getValue()).map(adGroupAdResult -> (ResponsiveDisplayAd) adGroupAdResult.getAd()).forEach(newAd -> System.out.printf("Responsive display ad with ID %d and short headline '%s' was added.%n", newAd.getId(), newAd.getShortHeadline()));
}
Also used : ResponsiveDisplayAd(com.google.api.ads.adwords.axis.v201809.cm.ResponsiveDisplayAd) Arrays(java.util.Arrays) Parameter(com.beust.jcommander.Parameter) ResponsiveDisplayAd(com.google.api.ads.adwords.axis.v201809.cm.ResponsiveDisplayAd) OfflineCredentials(com.google.api.ads.common.lib.auth.OfflineCredentials) ApiException(com.google.api.ads.adwords.axis.v201809.cm.ApiException) ArgumentNames(com.google.api.ads.adwords.lib.utils.examples.ArgumentNames) Credential(com.google.api.client.auth.oauth2.Credential) AdGroupAdReturnValue(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdReturnValue) AdGroupAdStatus(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdStatus) OAuthException(com.google.api.ads.common.lib.exception.OAuthException) MediaMediaType(com.google.api.ads.adwords.axis.v201809.cm.MediaMediaType) AdWordsServices(com.google.api.ads.adwords.axis.factory.AdWordsServices) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) MediaServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.MediaServiceInterface) IOException(java.io.IOException) Operator(com.google.api.ads.adwords.axis.v201809.cm.Operator) CodeSampleParams(com.google.api.ads.common.lib.utils.examples.CodeSampleParams) RemoteException(java.rmi.RemoteException) DEFAULT_CONFIGURATION_FILENAME(com.google.api.ads.common.lib.utils.Builder.DEFAULT_CONFIGURATION_FILENAME) AdGroupAd(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd) Media(com.google.api.ads.adwords.axis.v201809.cm.Media) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) AdWordsServicesInterface(com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface) ApiError(com.google.api.ads.adwords.axis.v201809.cm.ApiError) ValidationException(com.google.api.ads.common.lib.exception.ValidationException) AdGroupAdOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation) DynamicSettings(com.google.api.ads.adwords.axis.v201809.cm.DynamicSettings) Api(com.google.api.ads.common.lib.auth.OfflineCredentials.Api) Image(com.google.api.ads.adwords.axis.v201809.cm.Image) AdGroupAdServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface) AdGroupAd(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd) AdGroupAdReturnValue(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdReturnValue) MediaServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.MediaServiceInterface) AdGroupAdServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface) AdGroupAdOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation) Image(com.google.api.ads.adwords.axis.v201809.cm.Image) DynamicSettings(com.google.api.ads.adwords.axis.v201809.cm.DynamicSettings)

Aggregations

AdWordsServices (com.google.api.ads.adwords.axis.factory.AdWordsServices)9 ApiError (com.google.api.ads.adwords.axis.v201809.cm.ApiError)9 ApiException (com.google.api.ads.adwords.axis.v201809.cm.ApiException)9 AdWordsSession (com.google.api.ads.adwords.lib.client.AdWordsSession)9 AdWordsServicesInterface (com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface)9 OfflineCredentials (com.google.api.ads.common.lib.auth.OfflineCredentials)9 Api (com.google.api.ads.common.lib.auth.OfflineCredentials.Api)9 ConfigurationLoadException (com.google.api.ads.common.lib.conf.ConfigurationLoadException)9 OAuthException (com.google.api.ads.common.lib.exception.OAuthException)9 ValidationException (com.google.api.ads.common.lib.exception.ValidationException)9 DEFAULT_CONFIGURATION_FILENAME (com.google.api.ads.common.lib.utils.Builder.DEFAULT_CONFIGURATION_FILENAME)9 Credential (com.google.api.client.auth.oauth2.Credential)9 RemoteException (java.rmi.RemoteException)9 Parameter (com.beust.jcommander.Parameter)6 Operator (com.google.api.ads.adwords.axis.v201809.cm.Operator)6 List (java.util.List)6 ArgumentNames (com.google.api.ads.adwords.lib.utils.examples.ArgumentNames)5 CodeSampleParams (com.google.api.ads.common.lib.utils.examples.CodeSampleParams)5 ArrayList (java.util.ArrayList)5 Arrays (java.util.Arrays)5