Search in sources :

Example 71 with ApiException

use of com.google.api.ads.admanager.axis.v202108.ApiException in project googleads-java-lib by googleads.

the class GetTopLevelAdUnits method main.

public static void main(String[] args) {
    AdManagerSession session;
    try {
        // Generate a refreshable OAuth2 credential.
        Credential oAuth2Credential = new OfflineCredentials.Builder().forApi(Api.AD_MANAGER).fromFile().build().generateCredential();
        // Construct a AdManagerSession.
        session = new AdManagerSession.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;
    }
    AdManagerServices adManagerServices = new AdManagerServices();
    try {
        runExample(adManagerServices, 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 admanager.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) StatementBuilder(com.google.api.ads.admanager.axis.utils.v202108.StatementBuilder) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) OAuthException(com.google.api.ads.common.lib.exception.OAuthException) ApiError(com.google.api.ads.admanager.axis.v202108.ApiError) RemoteException(java.rmi.RemoteException) AdManagerSession(com.google.api.ads.admanager.lib.client.AdManagerSession) AdManagerServices(com.google.api.ads.admanager.axis.factory.AdManagerServices) ApiException(com.google.api.ads.admanager.axis.v202108.ApiException)

Example 72 with ApiException

use of com.google.api.ads.admanager.axis.v202108.ApiException in project googleads-java-lib by googleads.

the class CreateLabels method main.

public static void main(String[] args) {
    AdManagerSession session;
    try {
        // Generate a refreshable OAuth2 credential.
        Credential oAuth2Credential = new OfflineCredentials.Builder().forApi(Api.AD_MANAGER).fromFile().build().generateCredential();
        // Construct a AdManagerSession.
        session = new AdManagerSession.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;
    }
    AdManagerServices adManagerServices = new AdManagerServices();
    try {
        runExample(adManagerServices, 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 admanager.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) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) OAuthException(com.google.api.ads.common.lib.exception.OAuthException) ApiError(com.google.api.ads.admanager.axis.v202108.ApiError) RemoteException(java.rmi.RemoteException) AdManagerSession(com.google.api.ads.admanager.lib.client.AdManagerSession) AdManagerServices(com.google.api.ads.admanager.axis.factory.AdManagerServices) ApiException(com.google.api.ads.admanager.axis.v202108.ApiException)

Example 73 with ApiException

use of com.google.api.ads.admanager.axis.v202108.ApiException in project googleads-java-lib by googleads.

the class GetActiveLabels method runExample.

/**
 * Runs the example.
 *
 * @param adManagerServices 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(AdManagerServices adManagerServices, AdManagerSession session) throws RemoteException {
    LabelServiceInterface labelService = adManagerServices.get(session, LabelServiceInterface.class);
    // Create a statement to select labels.
    StatementBuilder statementBuilder = new StatementBuilder().where("isActive = :isActive").orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT).withBindVariableValue("isActive", true);
    // Retrieve a small amount of labels at a time, paging through
    // until all labels have been retrieved.
    int totalResultSetSize = 0;
    do {
        LabelPage page = labelService.getLabelsByStatement(statementBuilder.toStatement());
        if (page.getResults() != null) {
            // Print out some information for each label.
            totalResultSetSize = page.getTotalResultSetSize();
            int i = page.getStartIndex();
            for (Label label : page.getResults()) {
                System.out.printf("%d) Label with ID %d and name '%s' was found.%n", i++, label.getId(), label.getName());
            }
        }
        statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    } while (statementBuilder.getOffset() < totalResultSetSize);
    System.out.printf("Number of results found: %d%n", totalResultSetSize);
}
Also used : LabelPage(com.google.api.ads.admanager.axis.v202108.LabelPage) StatementBuilder(com.google.api.ads.admanager.axis.utils.v202108.StatementBuilder) LabelServiceInterface(com.google.api.ads.admanager.axis.v202108.LabelServiceInterface) Label(com.google.api.ads.admanager.axis.v202108.Label)

Example 74 with ApiException

use of com.google.api.ads.admanager.axis.v202108.ApiException in project googleads-java-lib by googleads.

the class UpdateLabels method runExample.

/**
 * Runs the example.
 *
 * @param adManagerServices the services factory.
 * @param session the session.
 * @param labelId the ID of the label to update.
 * @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(AdManagerServices adManagerServices, AdManagerSession session, long labelId) throws RemoteException {
    // Get the LabelService.
    LabelServiceInterface labelService = adManagerServices.get(session, LabelServiceInterface.class);
    // Create a statement to only select a single label by ID.
    StatementBuilder statementBuilder = new StatementBuilder().where("id = :id").orderBy("id ASC").limit(1).withBindVariableValue("id", labelId);
    // Get the label.
    LabelPage page = labelService.getLabelsByStatement(statementBuilder.toStatement());
    Label label = Iterables.getOnlyElement(Arrays.asList(page.getResults()));
    // Update the label description.
    label.setDescription("New label description");
    // Update the label on the server.
    Label[] labels = labelService.updateLabels(new Label[] { label });
    for (Label updatedLabel : labels) {
        System.out.printf("Label with ID %d and name '%s' was updated.%n", updatedLabel.getId(), updatedLabel.getName());
    }
}
Also used : LabelPage(com.google.api.ads.admanager.axis.v202108.LabelPage) StatementBuilder(com.google.api.ads.admanager.axis.utils.v202108.StatementBuilder) LabelServiceInterface(com.google.api.ads.admanager.axis.v202108.LabelServiceInterface) Label(com.google.api.ads.admanager.axis.v202108.Label)

Example 75 with ApiException

use of com.google.api.ads.admanager.axis.v202108.ApiException in project googleads-java-lib by googleads.

the class GetLicasForLineItem method runExample.

/**
 * Runs the example.
 *
 * @param adManagerServices the services factory.
 * @param session the session.
 * @param lineItemId the ID of the line item.
 * @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(AdManagerServices adManagerServices, AdManagerSession session, long lineItemId) throws RemoteException {
    LineItemCreativeAssociationServiceInterface lineItemCreativeAssociationService = adManagerServices.get(session, LineItemCreativeAssociationServiceInterface.class);
    // Create a statement to select line item creative associations.
    StatementBuilder statementBuilder = new StatementBuilder().where("lineItemId = :lineItemId").orderBy("lineItemId ASC, creativeId ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT).withBindVariableValue("lineItemId", lineItemId);
    // Retrieve a small amount of line item creative associations at a time, paging through
    // until all line item creative associations have been retrieved.
    int totalResultSetSize = 0;
    do {
        LineItemCreativeAssociationPage page = lineItemCreativeAssociationService.getLineItemCreativeAssociationsByStatement(statementBuilder.toStatement());
        if (page.getResults() != null) {
            // Print out some information for each line item creative association.
            totalResultSetSize = page.getTotalResultSetSize();
            int i = page.getStartIndex();
            for (LineItemCreativeAssociation lica : page.getResults()) {
                if (lica.getCreativeSetId() != null) {
                    System.out.printf("%d) LICA with line item ID %d and creative set ID %d was found.%n", i++, lica.getLineItemId(), lica.getCreativeSetId());
                } else {
                    System.out.printf("%d) LICA with line item ID %d and creative ID %d was found.%n", i++, lica.getLineItemId(), lica.getCreativeId());
                }
            }
        }
        statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    } while (statementBuilder.getOffset() < totalResultSetSize);
    System.out.printf("Number of results found: %d%n", totalResultSetSize);
}
Also used : LineItemCreativeAssociation(com.google.api.ads.admanager.axis.v202108.LineItemCreativeAssociation) StatementBuilder(com.google.api.ads.admanager.axis.utils.v202108.StatementBuilder) LineItemCreativeAssociationPage(com.google.api.ads.admanager.axis.v202108.LineItemCreativeAssociationPage) LineItemCreativeAssociationServiceInterface(com.google.api.ads.admanager.axis.v202108.LineItemCreativeAssociationServiceInterface)

Aggregations

ConfigurationLoadException (com.google.api.ads.common.lib.conf.ConfigurationLoadException)490 OAuthException (com.google.api.ads.common.lib.exception.OAuthException)490 ValidationException (com.google.api.ads.common.lib.exception.ValidationException)490 RemoteException (java.rmi.RemoteException)490 AdManagerServices (com.google.api.ads.admanager.axis.factory.AdManagerServices)487 AdManagerSession (com.google.api.ads.admanager.lib.client.AdManagerSession)487 Credential (com.google.api.client.auth.oauth2.Credential)487 StatementBuilder (com.google.api.ads.admanager.axis.utils.v202108.StatementBuilder)233 ApiException (com.google.api.ads.admanager.axis.v202202.ApiException)164 ApiException (com.google.api.ads.admanager.axis.v202108.ApiException)163 ApiException (com.google.api.ads.admanager.axis.v202111.ApiException)163 ApiError (com.google.api.ads.admanager.axis.v202202.ApiError)163 ApiError (com.google.api.ads.admanager.axis.v202108.ApiError)162 ApiError (com.google.api.ads.admanager.axis.v202111.ApiError)162 StatementBuilder (com.google.api.ads.admanager.axis.utils.v202111.StatementBuilder)117 StatementBuilder (com.google.api.ads.admanager.axis.utils.v202202.StatementBuilder)117 IOException (java.io.IOException)59 Random (java.util.Random)25 UpdateResult (com.google.api.ads.admanager.axis.v202108.UpdateResult)18 NetworkServiceInterface (com.google.api.ads.admanager.axis.v202108.NetworkServiceInterface)15