use of com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface 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();
}
}
use of com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface in project googleads-java-lib by googleads.
the class RemoveAdGroup 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();
RemoveAdGroupParams params = new RemoveAdGroupParams();
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);
}
}
use of com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface in project googleads-java-lib by googleads.
the class RemoveKeyword 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();
RemoveKeywordParams params = new RemoveKeywordParams();
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.criterionId = Long.parseLong("INSERT_CRITERION_ID_HERE");
}
try {
runExample(adWordsServices, session, params.adGroupId, params.criterionId);
} 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);
}
}
use of com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface in project googleads-java-lib by googleads.
the class UploadOfflineData 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();
UploadOfflineDataParams params = new UploadOfflineDataParams();
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.emailAddresses = Arrays.asList("INSERT_EMAIL_ADDRESS_1_HERE", "INSERT_EMAIL_ADDRESS_2_HERE");
params.conversionName = "INSERT_CONVERSION_NAME_HERE";
params.externalUploadId = Long.valueOf("INSERT_EXTERNAL_UPLOAD_ID_HERE");
// Change the enum below to STORE_SALES_UPLOAD_THIRD_PARTY if uploading third party data.
params.offlineDataUploadType = OfflineDataUploadType.STORE_SALES_UPLOAD_FIRST_PARTY.getValue();
// The three constants below are needed when uploading third party data.
// You can safely ignore them if you are uploading first party data.
// For times, use the format yyyyMMdd HHmmss tz. For more details on formats, see:
// https://developers.google.com/adwords/api/docs/appendix/codes-formats#date-and-time-formats
// For time zones, see:
// https://developers.google.com/adwords/api/docs/appendix/codes-formats#timezone-ids
params.advertiserUploadTime = "INSERT_ADVERTISER_UPLOAD_TIME";
params.bridgeMapVersionId = "INSERT_BRIDGE_MAP_VERSION_ID";
params.partnerId = Integer.valueOf("INSERT_PARTNER_ID");
}
OfflineDataUploadType uploadTypeEnum = OfflineDataUploadType.fromString(params.offlineDataUploadType);
// Set partial failure to true since this example demonstrates how to handle partial
// failure errors.
session.setPartialFailure(true);
try {
runExample(adWordsServices, session, params.conversionName, params.externalUploadId, params.emailAddresses, uploadTypeEnum, params.advertiserUploadTime, params.bridgeMapVersionId, params.partnerId);
} 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 (UnsupportedEncodingException ue) {
System.err.printf("Example failed due to encoding exception: %s%n", ue);
}
}
use of com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface in project googleads-java-lib by googleads.
the class DownloadCriteriaReportWithSelector 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();
// Location to download report to.
String reportFile = System.getProperty("user.home") + File.separatorChar + "report.csv";
try {
runExample(adWordsServices, session, reportFile);
} catch (DetailedReportDownloadResponseException dre) {
// A DetailedReportDownloadResponseException will be thrown if the HTTP status code in the
// response indicates an error occurred and the response body contains XML with further
// information, such as the fieldPath and trigger.
System.err.printf("Report was not downloaded due to a %s with errorText '%s', trigger '%s' and " + "field path '%s'%n", dre.getClass().getSimpleName(), dre.getErrorText(), dre.getTrigger(), dre.getFieldPath());
} catch (ReportDownloadResponseException rde) {
// A ReportDownloadResponseException will be thrown if the HTTP status code in the response
// indicates an error occurred, but the response did not contain further details.
System.err.printf("Report was not downloaded due to: %s%n", rde);
} catch (ReportException re) {
// A ReportException will be thrown if the download failed due to a transport layer exception.
System.err.printf("Report was not downloaded due to transport layer exception: %s%n", re);
} catch (IOException ioe) {
// An IOException in this example indicates that the report's contents could not be written
// to the output file.
System.err.printf("Report was not written to file %s due to an IOException: %s%n", reportFile, ioe);
}
}
Aggregations