Search in sources :

Example 86 with ApiException

use of com.google.api.ads.adwords.axis.v201809.cm.ApiException in project googleads-java-lib by googleads.

the class AddDynamicPageFeed method main.

public static void main(String[] args) {
    AdWordsSession session;
    try {
        // Generate a refreshable OAuth2 credential.
        Credential oAuth2Credential = new OfflineCredentials.Builder().forApi(Api.ADWORDS).fromFile().build().generateCredential();
        // Construct an AdWordsSession.
        session = new AdWordsSession.Builder().fromFile().withOAuth2Credential(oAuth2Credential).build();
    } catch (ConfigurationLoadException cle) {
        System.err.printf("Failed to load configuration from the %s file. Exception: %s%n", DEFAULT_CONFIGURATION_FILENAME, cle);
        return;
    } catch (ValidationException ve) {
        System.err.printf("Invalid configuration in the %s file. Exception: %s%n", DEFAULT_CONFIGURATION_FILENAME, ve);
        return;
    } catch (OAuthException oe) {
        System.err.printf("Failed to create OAuth credentials. Check OAuth settings in the %s file. " + "Exception: %s%n", DEFAULT_CONFIGURATION_FILENAME, oe);
        return;
    }
    AdWordsServicesInterface adWordsServices = AdWordsServices.getInstance();
    AddDynamicPageFeedParams params = new AddDynamicPageFeedParams();
    if (!params.parseArguments(args)) {
        // Either pass the required parameters for this example on the command line, or insert them
        // into the code here. See the parameter class definition above for descriptions.
        params.campaignId = Long.parseLong("INSERT_CAMPAIGN_ID_HERE");
        params.adGroupId = Long.parseLong("INSERT_AD_GROUP_ID_HERE");
    }
    try {
        runExample(adWordsServices, session, params.campaignId, params.adGroupId);
    } catch (ApiException apiException) {
        // ApiException is the base class for most exceptions thrown by an API request. Instances
        // of this exception have a message and a collection of ApiErrors that indicate the
        // type and underlying cause of the exception. Every exception object in the adwords.axis
        // packages will return a meaningful value from toString
        // 
        // ApiException extends RemoteException, so this catch block must appear before the
        // catch block for RemoteException.
        System.err.println("Request failed due to ApiException. Underlying ApiErrors:");
        if (apiException.getErrors() != null) {
            int i = 0;
            for (ApiError apiError : apiException.getErrors()) {
                System.err.printf("  Error %d: %s%n", i++, apiError);
            }
        }
    } catch (RemoteException re) {
        System.err.printf("Request failed unexpectedly due to RemoteException: %s%n", re);
    }
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) ValidationException(com.google.api.ads.common.lib.exception.ValidationException) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) OAuthException(com.google.api.ads.common.lib.exception.OAuthException) AdWordsServicesInterface(com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface) ApiError(com.google.api.ads.adwords.axis.v201809.cm.ApiError) RemoteException(java.rmi.RemoteException) ApiException(com.google.api.ads.adwords.axis.v201809.cm.ApiException)

Example 87 with ApiException

use of com.google.api.ads.adwords.axis.v201809.cm.ApiException in project googleads-java-lib by googleads.

the class AddDynamicPageFeed method addDsaTargeting.

/**
 * Sets custom targeting for the page feed URLs based on a list of labels.
 *
 * @param adWordsServices
 * @param session
 * @param adGroupId
 * @param dsaPageUrlLabel
 */
private static void addDsaTargeting(AdWordsServicesInterface adWordsServices, AdWordsSession session, Long adGroupId, String dsaPageUrlLabel) throws ApiException, RemoteException {
    // Get the AdGroupCriterionService.
    AdGroupCriterionServiceInterface adGroupCriterionService = adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
    // Create a webpage criterion.
    Webpage webpage = new Webpage();
    WebpageParameter parameter = new WebpageParameter();
    parameter.setCriterionName("Test criterion");
    webpage.setParameter(parameter);
    // Add a condition for label=specified_label_name.
    WebpageCondition condition = new WebpageCondition();
    condition.setOperand(WebpageConditionOperand.CUSTOM_LABEL);
    condition.setArgument(dsaPageUrlLabel);
    parameter.setConditions(new WebpageCondition[] { condition });
    BiddableAdGroupCriterion criterion = new BiddableAdGroupCriterion();
    criterion.setAdGroupId(adGroupId);
    criterion.setCriterion(webpage);
    // Set a custom bid for this criterion.
    BiddingStrategyConfiguration biddingStrategyConfiguration = new BiddingStrategyConfiguration();
    CpcBid cpcBid = new CpcBid();
    Money money = new Money();
    money.setMicroAmount(1_500_000L);
    cpcBid.setBid(money);
    biddingStrategyConfiguration.setBids(new Bids[] { cpcBid });
    criterion.setBiddingStrategyConfiguration(biddingStrategyConfiguration);
    AdGroupCriterionOperation operation = new AdGroupCriterionOperation();
    operation.setOperand(criterion);
    operation.setOperator(Operator.ADD);
    BiddableAdGroupCriterion newCriterion = (BiddableAdGroupCriterion) adGroupCriterionService.mutate(new AdGroupCriterionOperation[] { operation }).getValue(0);
    System.out.printf("Web page criterion with ID %d and status '%s' was created.%n", newCriterion.getCriterion().getId(), newCriterion.getUserStatus());
}
Also used : Money(com.google.api.ads.adwords.axis.v201809.cm.Money) AdGroupCriterionOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionOperation) Webpage(com.google.api.ads.adwords.axis.v201809.cm.Webpage) AdGroupCriterionServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionServiceInterface) BiddableAdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.BiddableAdGroupCriterion) BiddingStrategyConfiguration(com.google.api.ads.adwords.axis.v201809.cm.BiddingStrategyConfiguration) CpcBid(com.google.api.ads.adwords.axis.v201809.cm.CpcBid) WebpageParameter(com.google.api.ads.adwords.axis.v201809.cm.WebpageParameter) WebpageCondition(com.google.api.ads.adwords.axis.v201809.cm.WebpageCondition)

Example 88 with ApiException

use of com.google.api.ads.adwords.axis.v201809.cm.ApiException in project googleads-java-lib by googleads.

the class AddDynamicPageFeed method createFeed.

/**
 * Creates the feed for DSA page URLs.
 */
private static DSAFeedDetails createFeed(AdWordsServicesInterface adWordsServices, AdWordsSession session) throws ApiException, RemoteException {
    // Get the FeedService.
    FeedServiceInterface feedService = adWordsServices.get(session, FeedServiceInterface.class);
    // Create attributes.
    FeedAttribute urlAttribute = new FeedAttribute();
    urlAttribute.setType(FeedAttributeType.URL_LIST);
    urlAttribute.setName("Page URL");
    FeedAttribute labelAttribute = new FeedAttribute();
    labelAttribute.setType(FeedAttributeType.STRING_LIST);
    labelAttribute.setName("Label");
    // Create the feed.
    Feed dsaPageFeed = new Feed();
    dsaPageFeed.setName("DSA Feed #" + System.currentTimeMillis());
    dsaPageFeed.setAttributes(new FeedAttribute[] { urlAttribute, labelAttribute });
    dsaPageFeed.setOrigin(FeedOrigin.USER);
    // Create operation.
    FeedOperation operation = new FeedOperation();
    operation.setOperand(dsaPageFeed);
    operation.setOperator(Operator.ADD);
    // Add the feed.
    Feed newFeed = feedService.mutate(new FeedOperation[] { operation }).getValue(0);
    DSAFeedDetails feedDetails = new DSAFeedDetails();
    feedDetails.feedId = newFeed.getId();
    FeedAttribute[] savedAttributes = newFeed.getAttributes();
    feedDetails.urlAttributeId = savedAttributes[0].getId();
    feedDetails.labelAttributeId = savedAttributes[1].getId();
    System.out.printf("Feed with name '%s' and ID %d with urlAttributeId %d" + " and labelAttributeId %d was created.%n", newFeed.getName(), feedDetails.feedId, feedDetails.urlAttributeId, feedDetails.labelAttributeId);
    return feedDetails;
}
Also used : FeedAttribute(com.google.api.ads.adwords.axis.v201809.cm.FeedAttribute) FeedServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.FeedServiceInterface) FeedOperation(com.google.api.ads.adwords.axis.v201809.cm.FeedOperation) Feed(com.google.api.ads.adwords.axis.v201809.cm.Feed) PageFeed(com.google.api.ads.adwords.axis.v201809.cm.PageFeed)

Example 89 with ApiException

use of com.google.api.ads.adwords.axis.v201809.cm.ApiException in project googleads-java-lib by googleads.

the class AddExpandedTextAdWithUpgradedUrls 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);
    // Create expanded text ad with a tracking template and custom parameters.
    ExpandedTextAd expandedTextAd = new ExpandedTextAd();
    expandedTextAd.setHeadlinePart1("Luxury Cruise to Mars");
    expandedTextAd.setHeadlinePart2("Visit the Red Planet in style.");
    expandedTextAd.setDescription("Low-gravity fun for everyone!");
    // Specify a tracking url for 3rd party tracking provider. You may
    // specify one at customer, campaign, ad group, ad, criterion or
    // feed item levels.
    expandedTextAd.setTrackingUrlTemplate("http://tracker.example.com/?season={_season}&promocode={_promocode}&u={lpurl}");
    // Since your tracking url has two custom parameters, provide their
    // values too. This can be provided at campaign, ad group, ad, criterion
    // or feed item levels.
    CustomParameter seasonParameter = new CustomParameter();
    seasonParameter.setKey("season");
    seasonParameter.setValue("christmas");
    CustomParameter promoCodeParameter = new CustomParameter();
    promoCodeParameter.setKey("promocode");
    promoCodeParameter.setValue("NYC123");
    CustomParameters trackingUrlParameters = new CustomParameters();
    trackingUrlParameters.setParameters(new CustomParameter[] { seasonParameter, promoCodeParameter });
    expandedTextAd.setUrlCustomParameters(trackingUrlParameters);
    // Specify a list of final urls. This field cannot be set if url field is
    // set. This may be specified at ad, criterion, and feed item levels.
    expandedTextAd.setFinalUrls(new String[] { "http://www.example.com/cruise/space/", "http://www.example.com/locations/mars/" });
    // Specify a list of final mobile urls. This field cannot be set if url field is
    // set or finalUrls is not set. This may be specified at ad, criterion, and feed
    // item levels.
    expandedTextAd.setFinalMobileUrls(new String[] { "http://mobile.example.com/cruise/space/", "http://mobile.example.com/locations/mars/" });
    // Create ad group ad.
    AdGroupAd textAdGroupAd = new AdGroupAd();
    textAdGroupAd.setAdGroupId(adGroupId);
    textAdGroupAd.setAd(expandedTextAd);
    // Optional: Set status.
    textAdGroupAd.setStatus(AdGroupAdStatus.PAUSED);
    // Create operation.
    AdGroupAdOperation textAdGroupAdOperation = new AdGroupAdOperation();
    textAdGroupAdOperation.setOperand(textAdGroupAd);
    textAdGroupAdOperation.setOperator(Operator.ADD);
    AdGroupAdOperation[] operations = new AdGroupAdOperation[] { textAdGroupAdOperation };
    // Add ad.
    AdGroupAd adGroupAdResult = adGroupAdService.mutate(operations).getValue(0);
    // Display ad.
    System.out.printf("Ad with ID %d and tracking URL template '%s' was added.", adGroupAdResult.getAd().getId(), adGroupAdResult.getAd().getTrackingUrlTemplate());
}
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) CustomParameter(com.google.api.ads.adwords.axis.v201809.cm.CustomParameter) CustomParameters(com.google.api.ads.adwords.axis.v201809.cm.CustomParameters)

Example 90 with ApiException

use of com.google.api.ads.adwords.axis.v201809.cm.ApiException in project googleads-java-lib by googleads.

the class AddExpandedTextAdWithUpgradedUrls method main.

public static void main(String[] args) {
    AdWordsSession session;
    try {
        // Generate a refreshable OAuth2 credential.
        Credential oAuth2Credential = new OfflineCredentials.Builder().forApi(Api.ADWORDS).fromFile().build().generateCredential();
        // Construct an AdWordsSession.
        session = new AdWordsSession.Builder().fromFile().withOAuth2Credential(oAuth2Credential).build();
    } catch (ConfigurationLoadException cle) {
        System.err.printf("Failed to load configuration from the %s file. Exception: %s%n", DEFAULT_CONFIGURATION_FILENAME, cle);
        return;
    } catch (ValidationException ve) {
        System.err.printf("Invalid configuration in the %s file. Exception: %s%n", DEFAULT_CONFIGURATION_FILENAME, ve);
        return;
    } catch (OAuthException oe) {
        System.err.printf("Failed to create OAuth credentials. Check OAuth settings in the %s file. " + "Exception: %s%n", DEFAULT_CONFIGURATION_FILENAME, oe);
        return;
    }
    AdWordsServicesInterface adWordsServices = AdWordsServices.getInstance();
    AddExpandedTextAdWithUpgradedUrlsParams params = new AddExpandedTextAdWithUpgradedUrlsParams();
    if (!params.parseArguments(args)) {
        // Either pass the required parameters for this example on the command line, or insert them
        // into the code here. See the parameter class definition above for descriptions.
        params.adGroupId = Long.parseLong("INSERT_AD_GROUP_ID_HERE");
    }
    try {
        runExample(adWordsServices, session, params.adGroupId);
    } catch (ApiException apiException) {
        // ApiException is the base class for most exceptions thrown by an API request. Instances
        // of this exception have a message and a collection of ApiErrors that indicate the
        // type and underlying cause of the exception. Every exception object in the adwords.axis
        // packages will return a meaningful value from toString
        // 
        // ApiException extends RemoteException, so this catch block must appear before the
        // catch block for RemoteException.
        System.err.println("Request failed due to ApiException. Underlying ApiErrors:");
        if (apiException.getErrors() != null) {
            int i = 0;
            for (ApiError apiError : apiException.getErrors()) {
                System.err.printf("  Error %d: %s%n", i++, apiError);
            }
        }
    } catch (RemoteException re) {
        System.err.printf("Request failed unexpectedly due to RemoteException: %s%n", re);
    }
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) ValidationException(com.google.api.ads.common.lib.exception.ValidationException) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) OAuthException(com.google.api.ads.common.lib.exception.OAuthException) AdWordsServicesInterface(com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface) ApiError(com.google.api.ads.adwords.axis.v201809.cm.ApiError) RemoteException(java.rmi.RemoteException) ApiException(com.google.api.ads.adwords.axis.v201809.cm.ApiException)

Aggregations

ApiException (com.google.api.ads.adwords.axis.v201809.cm.ApiException)102 ApiError (com.google.api.ads.adwords.axis.v201809.cm.ApiError)100 OAuthException (com.google.api.ads.common.lib.exception.OAuthException)100 ValidationException (com.google.api.ads.common.lib.exception.ValidationException)100 RemoteException (java.rmi.RemoteException)100 ConfigurationLoadException (com.google.api.ads.common.lib.conf.ConfigurationLoadException)99 AdWordsServicesInterface (com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface)98 AdWordsSession (com.google.api.ads.adwords.lib.client.AdWordsSession)97 Credential (com.google.api.client.auth.oauth2.Credential)97 SelectorBuilder (com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)47 ArrayList (java.util.ArrayList)23 Selector (com.google.api.ads.adwords.axis.v201809.cm.Selector)21 Money (com.google.api.ads.adwords.axis.v201809.cm.Money)18 AdGroupAd (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd)17 AdGroupAdServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface)16 Campaign (com.google.api.ads.adwords.axis.v201809.cm.Campaign)15 CampaignServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.CampaignServiceInterface)14 Budget (com.google.api.ads.adwords.axis.v201809.cm.Budget)13 BiddingStrategyConfiguration (com.google.api.ads.adwords.axis.v201809.cm.BiddingStrategyConfiguration)12 IOException (java.io.IOException)12