Search in sources :

Example 51 with ApiException

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

the class RemoveAd 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.
 * @param adId the ID of the ad to remove.
 * @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, long adId) throws RemoteException {
    // Get the AdGroupAdService.
    AdGroupAdServiceInterface adGroupAdService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
    // Create base class ad to avoid setting type specific fields.
    Ad ad = new Ad();
    ad.setId(adId);
    // Create ad group ad.
    AdGroupAd adGroupAd = new AdGroupAd();
    adGroupAd.setAdGroupId(adGroupId);
    adGroupAd.setAd(ad);
    // Create operations.
    AdGroupAdOperation operation = new AdGroupAdOperation();
    operation.setOperand(adGroupAd);
    operation.setOperator(Operator.REMOVE);
    AdGroupAdOperation[] operations = new AdGroupAdOperation[] { operation };
    // Remove ad.
    AdGroupAdReturnValue result = adGroupAdService.mutate(operations);
    // Display ads.
    for (AdGroupAd adGroupAdResult : result.getValue()) {
        System.out.printf("Ad with ID %d and type '%s' was removed.%n", adGroupAdResult.getAd().getId(), adGroupAdResult.getAd().getAdType());
    }
}
Also used : Ad(com.google.api.ads.adwords.axis.v201809.cm.Ad) AdGroupAd(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd) 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) AdGroupAdOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation)

Example 52 with ApiException

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

the class RemoveCampaign 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();
    RemoveCampaignParams params = new RemoveCampaignParams();
    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");
    }
    try {
        runExample(adWordsServices, session, params.campaignId);
    } 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)

Example 53 with ApiException

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

the class UpdateAdGroup 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();
    UpdateAdGroupParams params = new UpdateAdGroupParams();
    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");
        // Set this to null if you do not want to update the CPC bid.
        params.cpcBidMicroAmount = Long.parseLong("INSERT_CPC_BID_MICRO_AMOUNT_HERE");
    }
    try {
        runExample(adWordsServices, session, params.adGroupId, params.cpcBidMicroAmount);
    } 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)

Example 54 with ApiException

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

the class UpdateExpandedTextAd method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param adId the ID of the ad to update.
 * @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 adId) throws RemoteException {
    // Get the AdService.
    AdServiceInterface adService = adWordsServices.get(session, AdServiceInterface.class);
    // Creates an expanded text ad using the provided ad ID.
    ExpandedTextAd expandedTextAd = new ExpandedTextAd();
    expandedTextAd.setId(adId);
    // Updates some properties of the expanded text ad.
    expandedTextAd.setHeadlinePart1("Cruise to Pluto #" + System.currentTimeMillis());
    expandedTextAd.setHeadlinePart2("Tickets on sale now");
    expandedTextAd.setDescription("Best space cruise ever.");
    expandedTextAd.setFinalUrls(new String[] { "http://www.example.com/" });
    expandedTextAd.setFinalMobileUrls(new String[] { "http://www.example.com/mobile" });
    // Creates ad group ad operation and adds it to the list.
    AdOperation operation = new AdOperation();
    operation.setOperator(Operator.SET);
    operation.setOperand(expandedTextAd);
    // Updates the ad on the server.
    ExpandedTextAd updatedAd = (ExpandedTextAd) adService.mutate(new AdOperation[] { operation }).getValue(0);
    // Prints out some information.
    System.out.printf("Expanded text ad with ID %d was updated.%n", updatedAd.getId());
    System.out.printf("Headline part 1 is '%s'.%nHeadline part 2 is '%s'.%nDescription is '%s'.%n", updatedAd.getHeadlinePart1(), updatedAd.getHeadlinePart2(), updatedAd.getDescription());
    System.out.printf("Final URL is '%s'.%nFinal mobile URL is '%s'.%n", updatedAd.getFinalUrls()[0], updatedAd.getFinalMobileUrls()[0]);
}
Also used : ExpandedTextAd(com.google.api.ads.adwords.axis.v201809.cm.ExpandedTextAd) AdOperation(com.google.api.ads.adwords.axis.v201809.cm.AdOperation) AdServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdServiceInterface)

Example 55 with ApiException

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

the class AddCampaignGroupsAndPerformanceTargets method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param campaignIds the IDs of the campaigns to add to the campaign 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, List<Long> campaignIds) throws RemoteException {
    CampaignGroup campaignGroup = createCampaignGroup(adWordsServices, session);
    addCampaignsToGroup(adWordsServices, session, campaignGroup, campaignIds);
    createPerformanceTarget(adWordsServices, session, campaignGroup);
}
Also used : CampaignGroup(com.google.api.ads.adwords.axis.v201809.cm.CampaignGroup)

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