use of com.google.api.ads.adwords.axis.v201809.cm.ApiException in project googleads-java-lib by googleads.
the class AddGoogleMyBusinessLocationExtensions method main.
public static void main(String[] args) {
AdWordsSession session;
Credential oAuth2Credential;
try {
// Generate a refreshable OAuth2 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();
AddGoogleMyBusinessLocationExtensionsParams params = new AddGoogleMyBusinessLocationExtensionsParams();
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.gmbEmailAddress = "INSERT_GMB_EMAIL_ADDRESS_HERE";
params.gmbAccessToken = oAuth2Credential.getAccessToken();
params.businessAccountIdentifier = "INSERT_BUSINESS_ACCOUNT_IDENTIFIER_HERE";
}
try {
runExample(adWordsServices, session, params.gmbEmailAddress, params.gmbAccessToken, params.businessAccountIdentifier);
} 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);
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.ApiException in project googleads-java-lib by googleads.
the class AddCampaignLabels 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();
AddCampaignLabelsParams params = new AddCampaignLabelsParams();
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.campaignIds = Arrays.asList(Long.valueOf("INSERT_CAMPAIGN_ID_HERE"), Long.valueOf("INSERT_CAMPAIGN_ID_HERE"));
params.labelId = Long.parseLong("INSERT_LABEL_ID_HERE");
}
try {
runExample(adWordsServices, session, params.campaignIds, params.labelId);
} 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.axis.v201809.cm.ApiException in project googleads-java-lib by googleads.
the class AddTrial 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();
AddTrialParams params = new AddTrialParams();
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.draftId = Long.parseLong("INSERT_DRAFT_ID_HERE");
params.baseCampaignId = Long.parseLong("INSERT_BASE_CAMPAIGN_ID_HERE");
}
try {
runExample(adWordsServices, session, params.draftId, 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);
} catch (InterruptedException ie) {
System.err.printf("Thread was interrupted: %s%n", ie);
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.ApiException in project googleads-java-lib by googleads.
the class AddResponsiveSearchAd 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);
List<AdGroupAdOperation> operations = new ArrayList<>();
ResponsiveSearchAd responsiveSearchAd = new ResponsiveSearchAd();
List<AssetLink> headlines = new ArrayList<>();
AssetLink pinnedHeadline = createAssetLinkForText("Cruise to Mars #" + System.currentTimeMillis());
// Set a pinning to always choose this asset for HEADLINE_1. Pinning is optional; if no pinning
// is set, then headlines and descriptions will be rotated and the ones that perform best will
// be used more often.
pinnedHeadline.setPinnedField(ServedAssetFieldType.HEADLINE_1);
headlines.add(pinnedHeadline);
headlines.add(createAssetLinkForText("Best Space Cruise Line"));
headlines.add(createAssetLinkForText("Experience the Stars"));
responsiveSearchAd.setHeadlines(headlines.toArray(new AssetLink[0]));
List<AssetLink> descriptions = new ArrayList<>();
descriptions.add(createAssetLinkForText("Buy your tickets now"));
descriptions.add(createAssetLinkForText("Visit the Red Planet"));
responsiveSearchAd.setDescriptions(descriptions.toArray(new AssetLink[0]));
responsiveSearchAd.setFinalUrls(new String[] { "http://www.example.com/cruise" });
responsiveSearchAd.setPath1("all-inclusive");
responsiveSearchAd.setPath2("deals");
// Create ad group ad.
AdGroupAd responsiveSearchAdGroupAd = new AdGroupAd();
responsiveSearchAdGroupAd.setAdGroupId(adGroupId);
responsiveSearchAdGroupAd.setAd(responsiveSearchAd);
// Optional: set the status.
responsiveSearchAdGroupAd.setStatus(AdGroupAdStatus.PAUSED);
// Create the operation.
AdGroupAdOperation adGroupAdOperation = new AdGroupAdOperation();
adGroupAdOperation.setOperand(responsiveSearchAdGroupAd);
adGroupAdOperation.setOperator(Operator.ADD);
operations.add(adGroupAdOperation);
// Add ad.
AdGroupAdReturnValue result = adGroupAdService.mutate(operations.toArray(new AdGroupAdOperation[0]));
for (AdGroupAd adGroupAdResult : result.getValue()) {
ResponsiveSearchAd newAd = (ResponsiveSearchAd) adGroupAdResult.getAd();
System.out.printf("New responsive search ad with ID %d was added.%n", adGroupAdResult.getAd().getId());
System.out.println("Headlines:");
for (AssetLink headline : newAd.getHeadlines()) {
ServedAssetFieldType pinning = headline.getPinnedField();
System.out.printf(" %s%n", ((TextAsset) headline.getAsset()).getAssetText());
if (pinning != null) {
System.out.printf(" (pinned to %s)%n", pinning);
}
}
System.out.println("Descriptions:");
for (AssetLink description : newAd.getDescriptions()) {
ServedAssetFieldType pinning = description.getPinnedField();
System.out.printf(" %s%n", ((TextAsset) description.getAsset()).getAssetText());
if (pinning != null) {
System.out.printf(" (pinned to %s)%n", pinning);
}
}
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.ApiException in project googleads-java-lib by googleads.
the class AddResponsiveSearchAd 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();
AddResponsiveSearchAdParams params = new AddResponsiveSearchAdParams();
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);
}
}
Aggregations