Search in sources :

Example 86 with AdWordsServicesInterface

use of com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface in project googleads-java-lib by googleads.

the class AddCampaignGroupsAndPerformanceTargets 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();
    AddCampaignGroupsAndPerformanceTargetsParams params = new AddCampaignGroupsAndPerformanceTargetsParams();
    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"));
    }
    try {
        runExample(adWordsServices, session, params.campaignIds);
    } 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);
    }
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) ValidationException(com.google.api.ads.common.lib.exception.ValidationException) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) OAuthException(com.google.api.ads.common.lib.exception.OAuthException) AdWordsServicesInterface(com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface) ApiError(com.google.api.ads.adwords.axis.v201809.cm.ApiError) RemoteException(java.rmi.RemoteException) ApiException(com.google.api.ads.adwords.axis.v201809.cm.ApiException)

Example 87 with AdWordsServicesInterface

use of com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface in project googleads-java-lib by googleads.

the class MigrateToExtensionSettings 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);
    }
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) ValidationException(com.google.api.ads.common.lib.exception.ValidationException) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) OAuthException(com.google.api.ads.common.lib.exception.OAuthException) AdWordsServicesInterface(com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface) ApiError(com.google.api.ads.adwords.axis.v201809.cm.ApiError) RemoteException(java.rmi.RemoteException) ApiException(com.google.api.ads.adwords.axis.v201809.cm.ApiException)

Example 88 with AdWordsServicesInterface

use of com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface in project googleads-java-lib by googleads.

the class GetAllImagesAndVideos 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);
    }
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) ValidationException(com.google.api.ads.common.lib.exception.ValidationException) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) OAuthException(com.google.api.ads.common.lib.exception.OAuthException) AdWordsServicesInterface(com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface) ApiError(com.google.api.ads.adwords.axis.v201809.cm.ApiError) RemoteException(java.rmi.RemoteException) ApiException(com.google.api.ads.adwords.axis.v201809.cm.ApiException)

Example 89 with AdWordsServicesInterface

use of com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface in project googleads-java-lib by googleads.

the class EstimateKeywordTraffic method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @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) throws RemoteException {
    // Get the TrafficEstimatorService.
    TrafficEstimatorServiceInterface trafficEstimatorService = adWordsServices.get(session, TrafficEstimatorServiceInterface.class);
    // Create keywords. Refer to the TrafficEstimatorService documentation for the maximum
    // number of keywords that can be passed in a single request.
    // https://developers.google.com/adwords/api/docs/reference/latest/TrafficEstimatorService
    List<Keyword> keywords = new ArrayList<Keyword>();
    Keyword marsCruiseKeyword = new Keyword();
    marsCruiseKeyword.setText("mars cruise");
    marsCruiseKeyword.setMatchType(KeywordMatchType.BROAD);
    keywords.add(marsCruiseKeyword);
    Keyword cheapCruiseKeyword = new Keyword();
    cheapCruiseKeyword.setText("cheap cruise");
    cheapCruiseKeyword.setMatchType(KeywordMatchType.PHRASE);
    keywords.add(cheapCruiseKeyword);
    Keyword cruiseKeyword = new Keyword();
    cruiseKeyword.setText("cruise");
    cruiseKeyword.setMatchType(KeywordMatchType.EXACT);
    keywords.add(cruiseKeyword);
    // Create a keyword estimate request for each keyword.
    List<KeywordEstimateRequest> keywordEstimateRequests = keywords.stream().map(keyword -> {
        KeywordEstimateRequest keywordEstimateRequest = new KeywordEstimateRequest();
        keywordEstimateRequest.setKeyword(keyword);
        return keywordEstimateRequest;
    }).collect(Collectors.toList());
    // Add a negative keyword to the traffic estimate.
    KeywordEstimateRequest negativeKeywordEstimateRequest = new KeywordEstimateRequest();
    negativeKeywordEstimateRequest.setKeyword(new Keyword(null, null, null, "hiking tour", KeywordMatchType.BROAD));
    negativeKeywordEstimateRequest.setIsNegative(true);
    keywordEstimateRequests.add(negativeKeywordEstimateRequest);
    // Create ad group estimate requests.
    List<AdGroupEstimateRequest> adGroupEstimateRequests = new ArrayList<AdGroupEstimateRequest>();
    AdGroupEstimateRequest adGroupEstimateRequest = new AdGroupEstimateRequest();
    adGroupEstimateRequest.setKeywordEstimateRequests(keywordEstimateRequests.toArray(new KeywordEstimateRequest[] {}));
    adGroupEstimateRequest.setMaxCpc(new Money(null, 1000000L));
    adGroupEstimateRequests.add(adGroupEstimateRequest);
    // Create campaign estimate requests.
    List<CampaignEstimateRequest> campaignEstimateRequests = new ArrayList<CampaignEstimateRequest>();
    CampaignEstimateRequest campaignEstimateRequest = new CampaignEstimateRequest();
    campaignEstimateRequest.setAdGroupEstimateRequests(adGroupEstimateRequests.toArray(new AdGroupEstimateRequest[] {}));
    Location unitedStates = new Location();
    unitedStates.setId(2840L);
    Language english = new Language();
    english.setId(1000L);
    campaignEstimateRequest.setCriteria(new Criterion[] { unitedStates, english });
    campaignEstimateRequests.add(campaignEstimateRequest);
    // Create selector.
    TrafficEstimatorSelector selector = new TrafficEstimatorSelector();
    selector.setCampaignEstimateRequests(campaignEstimateRequests.toArray(new CampaignEstimateRequest[] {}));
    // Optional: Request a list of campaign level estimates segmented by platform.
    selector.setPlatformEstimateRequested(true);
    // Get traffic estimates.
    TrafficEstimatorResult result = trafficEstimatorService.get(selector);
    // Display traffic estimates.
    if (result != null && result.getCampaignEstimates() != null && result.getCampaignEstimates().length > 0) {
        CampaignEstimate campaignEstimate = result.getCampaignEstimates()[0];
        // Display the campaign level estimates segmented by platform.
        if (campaignEstimate.getPlatformEstimates() != null) {
            for (PlatformCampaignEstimate platformEstimate : campaignEstimate.getPlatformEstimates()) {
                String platformMessage = String.format("Results for the platform with ID %d and name '%s':%n", platformEstimate.getPlatform().getId(), platformEstimate.getPlatform().getPlatformName());
                displayMeanEstimates(platformMessage, platformEstimate.getMinEstimate(), platformEstimate.getMaxEstimate());
            }
        }
        // Display the keyword estimates.
        KeywordEstimate[] keywordEstimates = campaignEstimate.getAdGroupEstimates()[0].getKeywordEstimates();
        for (int i = 0; i < keywordEstimates.length; i++) {
            if (Boolean.TRUE.equals(keywordEstimateRequests.get(i).getIsNegative())) {
                continue;
            }
            Keyword keyword = keywordEstimateRequests.get(i).getKeyword();
            KeywordEstimate keywordEstimate = keywordEstimates[i];
            String keywordMessage = String.format("Results for the keyword with text '%s' and match type '%s':%n", keyword.getText(), keyword.getMatchType());
            displayMeanEstimates(keywordMessage, keywordEstimate.getMin(), keywordEstimate.getMax());
        }
    } else {
        System.out.println("No traffic estimates were returned.");
    }
}
Also used : TrafficEstimatorServiceInterface(com.google.api.ads.adwords.axis.v201809.o.TrafficEstimatorServiceInterface) TrafficEstimatorResult(com.google.api.ads.adwords.axis.v201809.o.TrafficEstimatorResult) KeywordMatchType(com.google.api.ads.adwords.axis.v201809.cm.KeywordMatchType) AdGroupEstimateRequest(com.google.api.ads.adwords.axis.v201809.o.AdGroupEstimateRequest) ArrayList(java.util.ArrayList) CampaignEstimate(com.google.api.ads.adwords.axis.v201809.o.CampaignEstimate) OfflineCredentials(com.google.api.ads.common.lib.auth.OfflineCredentials) ApiException(com.google.api.ads.adwords.axis.v201809.cm.ApiException) Money(com.google.api.ads.adwords.axis.v201809.cm.Money) Credential(com.google.api.client.auth.oauth2.Credential) OAuthException(com.google.api.ads.common.lib.exception.OAuthException) AdWordsServices(com.google.api.ads.adwords.axis.factory.AdWordsServices) Language(com.google.api.ads.adwords.axis.v201809.cm.Language) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) Keyword(com.google.api.ads.adwords.axis.v201809.cm.Keyword) Collectors(java.util.stream.Collectors) RemoteException(java.rmi.RemoteException) DEFAULT_CONFIGURATION_FILENAME(com.google.api.ads.common.lib.utils.Builder.DEFAULT_CONFIGURATION_FILENAME) List(java.util.List) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) AdWordsServicesInterface(com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface) ApiError(com.google.api.ads.adwords.axis.v201809.cm.ApiError) ValidationException(com.google.api.ads.common.lib.exception.ValidationException) CampaignEstimateRequest(com.google.api.ads.adwords.axis.v201809.o.CampaignEstimateRequest) KeywordEstimate(com.google.api.ads.adwords.axis.v201809.o.KeywordEstimate) Criterion(com.google.api.ads.adwords.axis.v201809.cm.Criterion) Api(com.google.api.ads.common.lib.auth.OfflineCredentials.Api) Location(com.google.api.ads.adwords.axis.v201809.cm.Location) KeywordEstimateRequest(com.google.api.ads.adwords.axis.v201809.o.KeywordEstimateRequest) PlatformCampaignEstimate(com.google.api.ads.adwords.axis.v201809.o.PlatformCampaignEstimate) TrafficEstimatorSelector(com.google.api.ads.adwords.axis.v201809.o.TrafficEstimatorSelector) StatsEstimate(com.google.api.ads.adwords.axis.v201809.o.StatsEstimate) CampaignEstimateRequest(com.google.api.ads.adwords.axis.v201809.o.CampaignEstimateRequest) Keyword(com.google.api.ads.adwords.axis.v201809.cm.Keyword) ArrayList(java.util.ArrayList) AdGroupEstimateRequest(com.google.api.ads.adwords.axis.v201809.o.AdGroupEstimateRequest) KeywordEstimateRequest(com.google.api.ads.adwords.axis.v201809.o.KeywordEstimateRequest) Money(com.google.api.ads.adwords.axis.v201809.cm.Money) PlatformCampaignEstimate(com.google.api.ads.adwords.axis.v201809.o.PlatformCampaignEstimate) Language(com.google.api.ads.adwords.axis.v201809.cm.Language) CampaignEstimate(com.google.api.ads.adwords.axis.v201809.o.CampaignEstimate) PlatformCampaignEstimate(com.google.api.ads.adwords.axis.v201809.o.PlatformCampaignEstimate) TrafficEstimatorServiceInterface(com.google.api.ads.adwords.axis.v201809.o.TrafficEstimatorServiceInterface) TrafficEstimatorResult(com.google.api.ads.adwords.axis.v201809.o.TrafficEstimatorResult) TrafficEstimatorSelector(com.google.api.ads.adwords.axis.v201809.o.TrafficEstimatorSelector) Location(com.google.api.ads.adwords.axis.v201809.cm.Location) KeywordEstimate(com.google.api.ads.adwords.axis.v201809.o.KeywordEstimate)

Example 90 with AdWordsServicesInterface

use of com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface in project googleads-java-lib by googleads.

the class GetCampaignsWithAwql 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);
    }
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) ValidationException(com.google.api.ads.common.lib.exception.ValidationException) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) OAuthException(com.google.api.ads.common.lib.exception.OAuthException) AdWordsServicesInterface(com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface) ApiError(com.google.api.ads.adwords.axis.v201809.cm.ApiError) RemoteException(java.rmi.RemoteException) ApiException(com.google.api.ads.adwords.axis.v201809.cm.ApiException)

Aggregations

AdWordsServicesInterface (com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface)104 AdWordsSession (com.google.api.ads.adwords.lib.client.AdWordsSession)102 ValidationException (com.google.api.ads.common.lib.exception.ValidationException)102 ConfigurationLoadException (com.google.api.ads.common.lib.conf.ConfigurationLoadException)101 OAuthException (com.google.api.ads.common.lib.exception.OAuthException)101 Credential (com.google.api.client.auth.oauth2.Credential)101 ApiError (com.google.api.ads.adwords.axis.v201809.cm.ApiError)98 ApiException (com.google.api.ads.adwords.axis.v201809.cm.ApiException)98 RemoteException (java.rmi.RemoteException)98 SelectorBuilder (com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)25 IOException (java.io.IOException)15 OfflineCredentials (com.google.api.ads.common.lib.auth.OfflineCredentials)11 AdWordsServices (com.google.api.ads.adwords.axis.factory.AdWordsServices)9 Api (com.google.api.ads.common.lib.auth.OfflineCredentials.Api)9 DEFAULT_CONFIGURATION_FILENAME (com.google.api.ads.common.lib.utils.Builder.DEFAULT_CONFIGURATION_FILENAME)9 List (java.util.List)7 Parameter (com.beust.jcommander.Parameter)6 Operator (com.google.api.ads.adwords.axis.v201809.cm.Operator)6 ArrayList (java.util.ArrayList)6 DetailedReportDownloadResponseException (com.google.api.ads.adwords.lib.utils.DetailedReportDownloadResponseException)5