private static final String ADVERTISER_ID = "INSERT_ADVERTISER_COMPANY_ID_HERE";
public static void runExample(DfpServices dfpServices, DfpSession session, long advertiserId)
throws Exception {
// Get the CreativeService.
CreativeServiceInterface creativeService =
dfpServices.get(session, CreativeServiceInterface.class);
// Create creative size.
Size size = new Size();
size.setWidth(300);
size.setHeight(250);
size.setIsAspectRatio(false);
// Create an image creative.
ImageCreative imageCreative = new ImageCreative();
imageCreative.setName("Image creative #" + new Random().nextInt(Integer.MAX_VALUE));
imageCreative.setAdvertiserId(advertiserId);
imageCreative.setDestinationUrl("http://google.com");
imageCreative.setSize(size);
// Create image asset.
CreativeAsset creativeAsset = new CreativeAsset();
creativeAsset.setFileName("image.jpg");
creativeAsset.setAssetByteArray(Media.getMediaDataFromUrl(
"http://www.google.com/intl/en/adwords/select/images/samples/inline.jpg"));
creativeAsset.setSize(size);
imageCreative.setPrimaryImageAsset(creativeAsset);
// Create an image redirect creative.
ImageRedirectCreative imageRedirectCreative = new ImageRedirectCreative();
imageRedirectCreative.setName(
"Image redirect creative #" + new Random().nextInt(Integer.MAX_VALUE));
imageRedirectCreative.setAdvertiserId(advertiserId);
imageRedirectCreative.setDestinationUrl("http://google.com");
imageRedirectCreative.setImageUrl(
"http://www.google.com/intl/en/adwords/select/images/samples/inline.jpg");
imageRedirectCreative.setSize(size);
// Create the creatives on the server.
Creative[] creatives =
creativeService.createCreatives(new Creative[] {imageCreative, imageRedirectCreative});
for (Creative createdCreative : creatives) {
System.out.printf("A creative with ID \"%d\", name \"%s\", and type \"%s\""
+ " was created and can be previewed at: %s\n", createdCreative.getId(),
createdCreative.getName(), createdCreative.getCreativeType(),