Search in sources :

Example 81 with ApiError

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

the class UpdateCampaign 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();
    UpdateCampaignParams params = new UpdateCampaignParams();
    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 82 with ApiError

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

the class UpdateKeyword 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();
    UpdateKeywordParams params = new UpdateKeywordParams();
    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");
        params.keywordId = Long.parseLong("INSERT_KEYWORD_ID_HERE");
    }
    try {
        runExample(adWordsServices, session, params.adGroupId, params.keywordId);
    } 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 83 with ApiError

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

the class AddDraft 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();
    AddDraftParams params = new AddDraftParams();
    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.baseCampaignId = Long.parseLong("INSERT_BASE_CAMPAIGN_ID_HERE");
    }
    try {
        runExample(adWordsServices, session, params.baseCampaignId);
    } 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 84 with ApiError

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

the class AddTrial method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param draftId the ID of the draft.
 * @param baseCampaignId the ID of the base campaign.
 * @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 InterruptedException
 */
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session, long draftId, long baseCampaignId) throws RemoteException, InterruptedException {
    // Get the TrialService.
    TrialServiceInterface trialService = adWordsServices.get(session, TrialServiceInterface.class);
    Trial trial = new Trial();
    trial.setDraftId(draftId);
    trial.setBaseCampaignId(baseCampaignId);
    trial.setName("Test Trial #" + System.currentTimeMillis());
    trial.setTrafficSplitPercent(50);
    trial.setTrafficSplitType(CampaignTrialTrafficSplitType.RANDOM_QUERY);
    TrialOperation trialOperation = new TrialOperation();
    trialOperation.setOperator(Operator.ADD);
    trialOperation.setOperand(trial);
    long trialId = trialService.mutate(new TrialOperation[] { trialOperation }).getValue(0).getId();
    // Since creating a trial is asynchronous, we have to poll it to wait for it to finish.
    Selector trialSelector = new SelectorBuilder().fields(TrialField.Id, TrialField.Status, TrialField.BaseCampaignId, TrialField.TrialCampaignId).equalsId(trialId).build();
    trial = null;
    boolean isPending = true;
    int pollAttempts = 0;
    do {
        long sleepSeconds = (long) Math.scalb(30d, pollAttempts);
        System.out.printf("Sleeping for %d seconds.%n", sleepSeconds);
        Thread.sleep(sleepSeconds * 1000);
        trial = trialService.get(trialSelector).getEntries(0);
        System.out.printf("Trial ID %d has status '%s'.%n", trial.getId(), trial.getStatus());
        pollAttempts++;
        isPending = TrialStatus.CREATING.equals(trial.getStatus());
    } while (isPending && pollAttempts < MAX_POLL_ATTEMPTS);
    if (TrialStatus.ACTIVE.equals(trial.getStatus())) {
        // The trial creation was successful.
        System.out.printf("Trial created with ID %d and trial campaign ID %d.%n", trial.getId(), trial.getTrialCampaignId());
    } else if (TrialStatus.CREATION_FAILED.equals(trial.getStatus())) {
        // The trial creation failed, and errors can be fetched from the TrialAsyncErrorService.
        Selector errorsSelector = new SelectorBuilder().fields(TrialAsyncErrorField.TrialId, TrialAsyncErrorField.AsyncError).equals(TrialAsyncErrorField.TrialId, trial.getId().toString()).build();
        TrialAsyncErrorServiceInterface trialAsyncErrorService = adWordsServices.get(session, TrialAsyncErrorServiceInterface.class);
        TrialAsyncErrorPage trialAsyncErrorPage = trialAsyncErrorService.get(errorsSelector);
        if (trialAsyncErrorPage.getEntries() == null || trialAsyncErrorPage.getEntries().length == 0) {
            System.out.printf("Could not retrieve errors for trial ID %d for draft ID %d.%n", trial.getId(), draftId);
        } else {
            System.out.printf("Could not create trial ID %d for draft ID %d due to the following errors:%n", trial.getId(), draftId);
            int i = 0;
            for (TrialAsyncError error : trialAsyncErrorPage.getEntries()) {
                ApiError asyncError = error.getAsyncError();
                System.out.printf("Error #%d: errorType='%s', errorString='%s', trigger='%s', fieldPath='%s'%n", i++, asyncError.getApiErrorType(), asyncError.getErrorString(), asyncError.getTrigger(), asyncError.getFieldPath());
            }
        }
    } else {
        // Most likely, the trial is still being created. You can continue polling,
        // but we have limited the number of attempts in the example.
        System.out.printf("Timed out waiting to create trial from draft ID %d with base campaign ID %d.%n", draftId, baseCampaignId);
    }
}
Also used : TrialServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.TrialServiceInterface) Trial(com.google.api.ads.adwords.axis.v201809.cm.Trial) SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) TrialAsyncErrorPage(com.google.api.ads.adwords.axis.v201809.cm.TrialAsyncErrorPage) ApiError(com.google.api.ads.adwords.axis.v201809.cm.ApiError) TrialAsyncErrorServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.TrialAsyncErrorServiceInterface) TrialOperation(com.google.api.ads.adwords.axis.v201809.cm.TrialOperation) TrialAsyncError(com.google.api.ads.adwords.axis.v201809.cm.TrialAsyncError) Selector(com.google.api.ads.adwords.axis.v201809.cm.Selector)

Example 85 with ApiError

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

the class AddSiteLinks 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();
    AddSiteLinksParams params = new AddSiteLinksParams();
    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)

Aggregations

ApiError (com.google.api.ads.adwords.axis.v201809.cm.ApiError)92 ApiException (com.google.api.ads.adwords.axis.v201809.cm.ApiException)90 AdWordsServicesInterface (com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface)90 OAuthException (com.google.api.ads.common.lib.exception.OAuthException)90 ValidationException (com.google.api.ads.common.lib.exception.ValidationException)90 RemoteException (java.rmi.RemoteException)90 AdWordsSession (com.google.api.ads.adwords.lib.client.AdWordsSession)89 ConfigurationLoadException (com.google.api.ads.common.lib.conf.ConfigurationLoadException)89 Credential (com.google.api.client.auth.oauth2.Credential)89 SelectorBuilder (com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)24 IOException (java.io.IOException)9 OfflineCredentials (com.google.api.ads.common.lib.auth.OfflineCredentials)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 BatchJobException (com.google.api.ads.adwords.lib.utils.BatchJobException)2 ArrayList (java.util.ArrayList)2 TimeoutException (java.util.concurrent.TimeoutException)2 Parameter (com.beust.jcommander.Parameter)1 AdWordsServices (com.google.api.ads.adwords.axis.factory.AdWordsServices)1 AdGroupCriterion (com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterion)1 AdGroupCriterionOperation (com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionOperation)1