use of com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface in project googleads-java-lib by googleads.
the class AddDynamicPageFeed 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();
AddDynamicPageFeedParams params = new AddDynamicPageFeedParams();
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");
params.adGroupId = Long.parseLong("INSERT_AD_GROUP_ID_HERE");
}
try {
runExample(adWordsServices, session, params.campaignId, 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 AddExpandedTextAdWithUpgradedUrls 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();
AddExpandedTextAdWithUpgradedUrlsParams params = new AddExpandedTextAdWithUpgradedUrlsParams();
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 AdvancedCreateCredentialFromScratch method main.
public static void main(String[] args) {
if (CLIENT_ID.equals("INSERT_CLIENT_ID_HERE") || CLIENT_SECRET.equals("INSERT_CLIENT_SECRET_HERE")) {
throw new IllegalArgumentException("Please input your client IDs or secret. " + "See https://console.developers.google.com/project");
}
// It is highly recommended that you use a credential store in your
// application to store a per-user Credential.
// See: https://developers.google.com/api-client-library/java/google-api-java-client/oauth2#data_store
DataStoreFactory storeFactory = new MemoryDataStoreFactory();
// Authorize and store your credential.
try {
authorize(storeFactory, USER_ID);
} catch (Exception e) {
System.err.printf("Failed to authorize credentials: %s%n", e);
return;
}
// Create a AdWordsSession from the credential store. You will typically do this
// in a servlet interceptor for a web application or per separate thread
// of your offline application.
AdWordsSession adWordsSession;
try {
adWordsSession = createAdWordsSession(USER_ID, storeFactory);
} 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 (IOException ioe) {
System.err.printf("Failed to load OAuth credentials from local data store. Exception: %s%n", ioe);
return;
}
AdWordsServicesInterface adWordsServices = AdWordsServices.getInstance();
// Location to download report to.
String reportFile = System.getProperty("user.home") + File.separatorChar + "report.csv";
try {
runExample(adWordsServices, adWordsSession, reportFile);
} 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 read from
// the response.
System.err.printf("Report was not read due to an IOException: %s%n", ioe);
}
}
use of com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface in project googleads-java-lib by googleads.
the class AddResponsiveDisplayAd 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.
* @throws IOException if uploading an image failed.
*/
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session, long adGroupId) throws IOException {
// Get the MediaService.
MediaServiceInterface mediaService = adWordsServices.get(session, MediaServiceInterface.class);
// Get the AdGroupAdService.
AdGroupAdServiceInterface adGroupAdService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
// Create a responsive display ad.
ResponsiveDisplayAd responsiveDisplayAd = new ResponsiveDisplayAd();
// This ad format does not allow the creation of an image using the
// Image.data field. An image must first be created using the MediaService,
// and Image.mediaId must be populated when creating the ad.
long marketingImageMediaId = uploadImage(mediaService, "https://goo.gl/3b9Wfh");
Image marketingImage = new Image();
marketingImage.setMediaId(marketingImageMediaId);
responsiveDisplayAd.setMarketingImage(marketingImage);
responsiveDisplayAd.setShortHeadline("Travel");
responsiveDisplayAd.setLongHeadline("Travel the World");
responsiveDisplayAd.setDescription("Take to the air!");
responsiveDisplayAd.setBusinessName("Interplanetary Cruises");
responsiveDisplayAd.setFinalUrls(new String[] { "http://www.example.com/" });
// Optional: Create a square marketing image using MediaService, and set it
// to the ad.
long squareMarketingImageMediaId = uploadImage(mediaService, "https://goo.gl/mtt54n");
Image squareMarketingImage = new Image();
squareMarketingImage.setMediaId(squareMarketingImageMediaId);
responsiveDisplayAd.setSquareMarketingImage(squareMarketingImage);
// Optional: set call to action text.
responsiveDisplayAd.setCallToActionText("Shop Now");
// Optional: Set dynamic display ad settings, composed of landscape logo
// image, promotion text, and price prefix.
DynamicSettings dynamicDisplayAdSettings = createDynamicDisplayAdSettings(mediaService);
responsiveDisplayAd.setDynamicDisplayAdSettings(dynamicDisplayAdSettings);
// Whitelisted accounts only: Set color settings using hexadecimal values.
// Set allowFlexibleColor to false if you want your ads to render by always
// using your colors strictly.
/*
responsiveDisplayAd.setMainColor("#0000ff");
responsiveDisplayAd.setAccentColor("#ffff00");
responsiveDisplayAd.setAllowFlexibleColor(false);
*/
// Whitelisted accounts only: Set the format setting that the ad will be
// served in.
/*
responsiveDisplayAd.setFormatSetting(
com.google.api.ads.adwords.axis.v201809.cm.DisplayAdFormatSetting.NON_NATIVE);
*/
// Create ad group ad for the responsive display ad.
AdGroupAd adGroupAd = new AdGroupAd();
adGroupAd.setAdGroupId(adGroupId);
adGroupAd.setAd(responsiveDisplayAd);
// Optional: set the status.
adGroupAd.setStatus(AdGroupAdStatus.PAUSED);
// Create the operation.
AdGroupAdOperation adGroupAdOperation = new AdGroupAdOperation();
adGroupAdOperation.setOperand(adGroupAd);
adGroupAdOperation.setOperator(Operator.ADD);
// Make the mutate request.
AdGroupAdReturnValue result = adGroupAdService.mutate(new AdGroupAdOperation[] { adGroupAdOperation });
// Display ads.
Arrays.stream(result.getValue()).map(adGroupAdResult -> (ResponsiveDisplayAd) adGroupAdResult.getAd()).forEach(newAd -> System.out.printf("Responsive display ad with ID %d and short headline '%s' was added.%n", newAd.getId(), newAd.getShortHeadline()));
}
use of com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface in project googleads-java-lib by googleads.
the class AddResponsiveDisplayAd 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();
AddResponsiveDisplayAdParams params = new AddResponsiveDisplayAdParams();
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 (IOException ioe) {
System.err.printf("Example failed due to IOException: %s%n", ioe);
}
}
Aggregations