use of com.google.api.ads.adwords.axis.v201809.cm.ApiException in project googleads-java-lib by googleads.
the class AddCompleteCampaignsUsingBatchJob method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @throws BatchJobException if uploading operations or downloading results failed.
* @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 if the thread was interrupted while sleeping between retries.
* @throws TimeoutException if the job did not complete after job status was polled {@link
* #MAX_POLL_ATTEMPTS} times.
*/
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session) throws RemoteException, BatchJobException, InterruptedException, TimeoutException {
// Get the MutateJobService.
BatchJobServiceInterface batchJobService = adWordsServices.get(session, BatchJobServiceInterface.class);
// Create a BatchJob.
BatchJobOperation addOp = new BatchJobOperation();
addOp.setOperator(Operator.ADD);
addOp.setOperand(new BatchJob());
BatchJob batchJob = batchJobService.mutate(new BatchJobOperation[] { addOp }).getValue(0);
// Get the upload URL from the new job.
String uploadUrl = batchJob.getUploadUrl().getUrl();
System.out.printf("Created BatchJob with ID %d, status '%s' and upload URL %s.%n", batchJob.getId(), batchJob.getStatus(), uploadUrl);
// Create a temporary ID generator that will produce a sequence of descending negative numbers.
Iterator<Long> tempIdGenerator = new AbstractSequentialIterator<Long>(-1L) {
@Override
protected Long computeNext(Long previous) {
return Long.MIN_VALUE == previous ? null : previous - 1;
}
};
// Use a random UUID name prefix to avoid name collisions.
String namePrefix = UUID.randomUUID().toString();
// Create the mutate request that will be sent to the upload URL.
List<Operation> operations = new ArrayList<>();
// Create and add an operation to create a new budget.
BudgetOperation budgetOperation = buildBudgetOperation(tempIdGenerator, namePrefix);
operations.add(budgetOperation);
// Create and add operations to create new campaigns.
List<CampaignOperation> campaignOperations = buildCampaignOperations(tempIdGenerator, namePrefix, budgetOperation);
operations.addAll(campaignOperations);
// Create and add operations to create new negative keyword criteria for each campaign.
operations.addAll(buildCampaignCriterionOperations(campaignOperations));
// Create and add operations to create new ad groups.
List<AdGroupOperation> adGroupOperations = new ArrayList<>(buildAdGroupOperations(tempIdGenerator, namePrefix, campaignOperations));
operations.addAll(adGroupOperations);
// Create and add operations to create new ad group criteria (keywords).
operations.addAll(buildAdGroupCriterionOperations(adGroupOperations));
// Create and add operations to create new ad group ads (text ads).
operations.addAll(buildAdGroupAdOperations(adGroupOperations));
// Use a BatchJobHelper to upload all operations.
BatchJobHelper batchJobHelper = adWordsServices.getUtility(session, BatchJobHelper.class);
batchJobHelper.uploadBatchJobOperations(operations, uploadUrl);
System.out.printf("Uploaded %d operations for batch job with ID %d.%n", operations.size(), batchJob.getId());
// Poll for completion of the batch job using an exponential back off.
int pollAttempts = 0;
boolean isPending;
Selector selector = new SelectorBuilder().fields(BatchJobField.Id, BatchJobField.Status, BatchJobField.DownloadUrl, BatchJobField.ProcessingErrors, BatchJobField.ProgressStats).equalsId(batchJob.getId()).build();
do {
long sleepSeconds = (long) Math.scalb(30, pollAttempts);
System.out.printf("Sleeping %d seconds...%n", sleepSeconds);
Thread.sleep(sleepSeconds * 1000);
batchJob = batchJobService.get(selector).getEntries(0);
System.out.printf("Batch job ID %d has status '%s'.%n", batchJob.getId(), batchJob.getStatus());
pollAttempts++;
isPending = PENDING_STATUSES.contains(batchJob.getStatus());
} while (isPending && pollAttempts < MAX_POLL_ATTEMPTS);
if (isPending) {
throw new TimeoutException("Job is still in pending state after polling " + MAX_POLL_ATTEMPTS + " times.");
}
if (batchJob.getProcessingErrors() != null) {
int i = 0;
for (BatchJobProcessingError processingError : batchJob.getProcessingErrors()) {
System.out.printf(" Processing error [%d]: errorType=%s, trigger=%s, errorString=%s, fieldPath=%s" + ", reason=%s%n", i++, processingError.getApiErrorType(), processingError.getTrigger(), processingError.getErrorString(), processingError.getFieldPath(), processingError.getReason());
}
} else {
System.out.println("No processing errors found.");
}
if (batchJob.getDownloadUrl() != null && batchJob.getDownloadUrl().getUrl() != null) {
BatchJobMutateResponse mutateResponse = batchJobHelper.downloadBatchJobMutateResponse(batchJob.getDownloadUrl().getUrl());
System.out.printf("Downloaded results from %s:%n", batchJob.getDownloadUrl().getUrl());
for (MutateResult mutateResult : mutateResponse.getMutateResults()) {
String outcome = mutateResult.getErrorList() == null ? "SUCCESS" : "FAILURE";
System.out.printf(" Operation [%d] - %s%n", mutateResult.getIndex(), outcome);
}
} else {
System.out.println("No results available for download.");
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.ApiException in project googleads-java-lib by googleads.
the class AddCompleteCampaignsUsingBatchJob 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();
try {
runExample(adWordsServices, session);
} 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);
} catch (InterruptedException ie) {
System.err.printf("Thread was interrupted: %s%n", ie);
} catch (TimeoutException te) {
System.err.printf("Job did not complete after status was polled %s times: %s%n", MAX_POLL_ATTEMPTS, te);
} catch (BatchJobException be) {
System.err.printf("Example failed due to BatchJobException: %s%n", be);
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.ApiException in project googleads-java-lib by googleads.
the class AddKeywordsUsingIncrementalBatchJob method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param adGroupId the ID of the ad group where keywords will be added.
* @throws BatchJobException if uploading operations or downloading results failed.
* @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 if the thread was interrupted while sleeping between retries.
* @throws TimeoutException if the job did not complete after job status was polled {@link
* #MAX_POLL_ATTEMPTS} times.
*/
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session, Long adGroupId) throws RemoteException, BatchJobException, InterruptedException, TimeoutException {
// Get the BatchJobService.
BatchJobServiceInterface batchJobService = adWordsServices.get(session, BatchJobServiceInterface.class);
BatchJobOperation addOp = new BatchJobOperation();
addOp.setOperator(Operator.ADD);
addOp.setOperand(new BatchJob());
BatchJob batchJob = batchJobService.mutate(new BatchJobOperation[] { addOp }).getValue(0);
System.out.printf("Created BatchJob with ID %d, status '%s' and upload URL %s.%n", batchJob.getId(), batchJob.getStatus(), batchJob.getUploadUrl().getUrl());
// Create a BatchJobHelper for uploading operations.
BatchJobHelper batchJobHelper = adWordsServices.getUtility(session, BatchJobHelper.class);
BatchJobUploadStatus batchJobUploadStatus = new BatchJobUploadStatus(0, URI.create(batchJob.getUploadUrl().getUrl()));
List<AdGroupCriterionOperation> operations = new ArrayList<>();
// incrementally.
for (int i = 0; i < NUMBER_OF_KEYWORDS_TO_ADD; i++) {
// Create Keyword.
String text = String.format("mars%d", i);
// Make 10% of keywords invalid to demonstrate error handling.
if (i % 10 == 0) {
text = text + "!!!";
}
Keyword keyword = new Keyword();
keyword.setText(text);
keyword.setMatchType(KeywordMatchType.BROAD);
// Create BiddableAdGroupCriterion.
BiddableAdGroupCriterion bagc = new BiddableAdGroupCriterion();
bagc.setAdGroupId(adGroupId);
bagc.setCriterion(keyword);
// Create AdGroupCriterionOperation.
AdGroupCriterionOperation agco = new AdGroupCriterionOperation();
agco.setOperand(bagc);
agco.setOperator(Operator.ADD);
// Add to the list of operations.
operations.add(agco);
// If the current list of operations has reached KEYWORDS_PER_UPLOAD or this is the last
// operation, upload the current list of operations.
boolean isLastOperation = i == NUMBER_OF_KEYWORDS_TO_ADD - 1;
if (operations.size() == KEYWORDS_PER_UPLOAD || isLastOperation) {
BatchJobUploadResponse uploadResponse = batchJobHelper.uploadIncrementalBatchJobOperations(operations, isLastOperation, batchJobUploadStatus);
System.out.printf("Uploaded %d operations for batch job with ID %d.%n", operations.size(), batchJob.getId());
// Set the batch job upload status and clear the operations list in preparation for the
// next upload.
batchJobUploadStatus = uploadResponse.getBatchJobUploadStatus();
operations.clear();
}
}
// Poll for completion of the batch job using an exponential back off.
int pollAttempts = 0;
boolean isPending;
boolean wasCancelRequested = false;
Selector selector = new SelectorBuilder().fields(BatchJobField.Id, BatchJobField.Status, BatchJobField.DownloadUrl, BatchJobField.ProcessingErrors, BatchJobField.ProgressStats).equalsId(batchJob.getId()).build();
do {
long sleepSeconds = (long) Math.scalb(30, pollAttempts);
System.out.printf("Sleeping %d seconds...%n", sleepSeconds);
Thread.sleep(sleepSeconds * 1000);
batchJob = batchJobService.get(selector).getEntries(0);
System.out.printf("Batch job ID %d has status '%s'.%n", batchJob.getId(), batchJob.getStatus());
pollAttempts++;
isPending = PENDING_STATUSES.contains(batchJob.getStatus());
// times.
if (isPending && !wasCancelRequested && pollAttempts == MAX_POLL_ATTEMPTS) {
batchJob.setStatus(BatchJobStatus.CANCELING);
BatchJobOperation batchJobSetOperation = new BatchJobOperation();
batchJobSetOperation.setOperand(batchJob);
batchJobSetOperation.setOperator(Operator.SET);
// Only request cancellation once per job.
wasCancelRequested = true;
try {
batchJob = batchJobService.mutate(new BatchJobOperation[] { batchJobSetOperation }).getValue(0);
System.out.printf("Requested cancellation of batch job with ID %d.%n", batchJob.getId());
} catch (ApiException e) {
if (e.getErrors() != null && e.getErrors().length > 0 && e.getErrors(0) instanceof BatchJobError) {
BatchJobError batchJobError = (BatchJobError) e.getErrors(0);
if (BatchJobErrorReason.INVALID_STATE_CHANGE.equals(batchJobError.getReason())) {
System.out.printf("Attempt to cancel batch job with ID %d was rejected because the job already " + "completed or was canceled.", batchJob.getId());
continue;
}
}
throw e;
} finally {
// Reset the poll attempt counter to wait for cancellation.
pollAttempts = 0;
}
}
} while (isPending && pollAttempts < MAX_POLL_ATTEMPTS);
if (isPending) {
throw new TimeoutException("Job is still in pending state after polling " + MAX_POLL_ATTEMPTS + " times.");
}
if (batchJob.getProcessingErrors() != null) {
int errorIndex = 0;
for (BatchJobProcessingError processingError : batchJob.getProcessingErrors()) {
System.out.printf(" Processing error [%d]: errorType=%s, trigger=%s, errorString=%s, fieldPath=%s" + ", reason=%s%n", errorIndex++, processingError.getApiErrorType(), processingError.getTrigger(), processingError.getErrorString(), processingError.getFieldPath(), processingError.getReason());
}
} else {
System.out.println("No processing errors found.");
}
if (batchJob.getDownloadUrl() != null && batchJob.getDownloadUrl().getUrl() != null) {
BatchJobMutateResponse mutateResponse = batchJobHelper.downloadBatchJobMutateResponse(batchJob.getDownloadUrl().getUrl());
System.out.printf("Downloaded results from %s:%n", batchJob.getDownloadUrl().getUrl());
for (MutateResult mutateResult : mutateResponse.getMutateResults()) {
String outcome = mutateResult.getErrorList() == null ? "SUCCESS" : "FAILURE";
System.out.printf(" Operation [%d] - %s%n", mutateResult.getIndex(), outcome);
}
} else {
System.out.println("No results available for download.");
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.ApiException in project googleads-java-lib by googleads.
the class AddKeywordsUsingIncrementalBatchJob 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();
AddKeywordsUsingIncrementalBatchJobParams params = new AddKeywordsUsingIncrementalBatchJobParams();
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);
} catch (InterruptedException ie) {
System.err.printf("Thread was interrupted: %s%n", ie);
} catch (TimeoutException te) {
System.err.printf("Job did not complete after status was polled %s times: %s%n", MAX_POLL_ATTEMPTS, te);
} catch (BatchJobException be) {
System.err.printf("Example failed due to BatchJobException: %s%n", be);
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.ApiException in project googleads-java-lib by googleads.
the class GetAllDisapprovedAds method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param adGroupId the ID of the ad group to search for disapproved ads.
* @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);
int offset = 0;
// Create selector.
SelectorBuilder builder = new SelectorBuilder();
Selector selector = builder.fields(AdGroupAdField.Id, AdGroupAdField.PolicySummary).orderAscBy(AdGroupAdField.Id).equals(AdGroupAdField.AdGroupId, adGroupId.toString()).equals(AdGroupAdField.CombinedApprovalStatus, PolicyApprovalStatus.DISAPPROVED.toString()).offset(offset).limit(PAGE_SIZE).build();
// Get all disapproved ads.
AdGroupAdPage page = null;
int disapprovedAdsCount = 0;
do {
page = adGroupAdService.get(selector);
// Display ads.
for (AdGroupAd adGroupAd : page) {
disapprovedAdsCount++;
AdGroupAdPolicySummary policySummary = adGroupAd.getPolicySummary();
System.out.printf("Ad with ID %d and type '%s' was disapproved with the following " + "policy topic entries:%n", adGroupAd.getAd().getId(), adGroupAd.getAd().getAdType());
// Display the policy topic entries related to the ad disapproval.
for (PolicyTopicEntry policyTopicEntry : policySummary.getPolicyTopicEntries()) {
System.out.printf(" topic id: %s, topic name: '%s', Help Center URL: %s%n", policyTopicEntry.getPolicyTopicId(), policyTopicEntry.getPolicyTopicName(), policyTopicEntry.getPolicyTopicHelpCenterUrl());
// Display the attributes and values that triggered the policy topic.
if (policyTopicEntry.getPolicyTopicEvidences() != null) {
for (PolicyTopicEvidence evidence : policyTopicEntry.getPolicyTopicEvidences()) {
System.out.printf(" evidence type: %s%n", evidence.getPolicyTopicEvidenceType());
if (evidence.getEvidenceTextList() != null) {
for (int i = 0; i < evidence.getEvidenceTextList().length; i++) {
System.out.printf(" evidence text[%d]: %s%n", i, evidence.getEvidenceTextList(i));
}
}
}
}
}
}
offset += PAGE_SIZE;
selector = builder.increaseOffsetBy(PAGE_SIZE).build();
} while (offset < page.getTotalNumEntries());
System.out.printf("%d disapproved ads were found.%n", disapprovedAdsCount);
}
Aggregations