Search in sources :

Example 76 with ApiException

use of com.google.api.ads.adwords.axis.v201809.cm.ApiException in project googleads-java-lib by googleads.

the class AddAudience 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 77 with ApiException

use of com.google.api.ads.adwords.axis.v201809.cm.ApiException in project googleads-java-lib by googleads.

the class AddCrmBasedUserList 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);
    } catch (UnsupportedEncodingException ue) {
        System.err.printf("Example failed due to encoding exception: %s%n", ue);
    }
}
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) UnsupportedEncodingException(java.io.UnsupportedEncodingException) 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 78 with ApiException

use of com.google.api.ads.adwords.axis.v201809.cm.ApiException in project googleads-java-lib by googleads.

the class AddCrmBasedUserList 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.
 * @throws UnsupportedEncodingException if encoding the hashed email failed.
 */
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session) throws RemoteException, UnsupportedEncodingException {
    // Get the UserListService.
    AdwordsUserListServiceInterface userListService = adWordsServices.get(session, AdwordsUserListServiceInterface.class);
    // Create a user list.
    CrmBasedUserList userList = new CrmBasedUserList();
    userList.setName("Customer relationship management list #" + System.currentTimeMillis());
    userList.setDescription("A list of customers that originated from email addresses");
    // CRM-based user lists can use a membershipLifeSpan of 10000 to indicate unlimited; otherwise
    // normal values apply.
    userList.setMembershipLifeSpan(30L);
    userList.setUploadKeyType(CustomerMatchUploadKeyType.CONTACT_INFO);
    // Create operation.
    UserListOperation operation = new UserListOperation();
    operation.setOperand(userList);
    operation.setOperator(Operator.ADD);
    // Add user list.
    UserListReturnValue result = userListService.mutate(new UserListOperation[] { operation });
    // Display user list.
    UserList userListAdded = result.getValue(0);
    System.out.printf("User list with name '%s' and ID %d was added.%n", userListAdded.getName(), userListAdded.getId());
    // Get user list ID.
    Long userListId = userListAdded.getId();
    // Create operation to add members to the user list based on email addresses.
    MutateMembersOperation mutateMembersOperation = new MutateMembersOperation();
    MutateMembersOperand operand = new MutateMembersOperand();
    operand.setUserListId(userListId);
    // Hash normalized email addresses based on SHA-256 hashing algorithm.
    List<Member> members = new ArrayList<>(EMAILS.size());
    for (String email : EMAILS) {
        String normalizedEmail = toNormalizedString(email);
        Member member = new Member();
        member.setHashedEmail(toSHA256String(normalizedEmail));
        members.add(member);
    }
    String firstName = "John";
    String lastName = "Doe";
    String countryCode = "US";
    String zipCode = "10011";
    AddressInfo addressInfo = new AddressInfo();
    // First and last name must be normalized and hashed.
    addressInfo.setHashedFirstName(toSHA256String(toNormalizedString(firstName)));
    addressInfo.setHashedLastName(toSHA256String(toNormalizedString(lastName)));
    // Country code and zip code are sent in plaintext.
    addressInfo.setCountryCode(countryCode);
    addressInfo.setZipCode(zipCode);
    Member memberByAddress = new Member();
    memberByAddress.setAddressInfo(addressInfo);
    members.add(memberByAddress);
    operand.setMembersList(members.toArray(new Member[members.size()]));
    mutateMembersOperation.setOperand(operand);
    mutateMembersOperation.setOperator(Operator.ADD);
    // Add members to the user list based on email addresses.
    MutateMembersReturnValue mutateMembersResult = userListService.mutateMembers(new MutateMembersOperation[] { mutateMembersOperation });
    // Reminder: it may take several hours for the list to be populated with members.
    for (UserList userListResult : mutateMembersResult.getUserLists()) {
        System.out.printf("%d email addresses were uploaded to user list with name '%s' and ID %d " + "and are scheduled for review.%n", EMAILS.size(), userListResult.getName(), userListResult.getId());
    }
}
Also used : AdwordsUserListServiceInterface(com.google.api.ads.adwords.axis.v201809.rm.AdwordsUserListServiceInterface) ArrayList(java.util.ArrayList) UserListOperation(com.google.api.ads.adwords.axis.v201809.rm.UserListOperation) UserListReturnValue(com.google.api.ads.adwords.axis.v201809.rm.UserListReturnValue) AddressInfo(com.google.api.ads.adwords.axis.v201809.rm.AddressInfo) CrmBasedUserList(com.google.api.ads.adwords.axis.v201809.rm.CrmBasedUserList) MutateMembersOperation(com.google.api.ads.adwords.axis.v201809.rm.MutateMembersOperation) MutateMembersOperand(com.google.api.ads.adwords.axis.v201809.rm.MutateMembersOperand) UserList(com.google.api.ads.adwords.axis.v201809.rm.UserList) CrmBasedUserList(com.google.api.ads.adwords.axis.v201809.rm.CrmBasedUserList) Member(com.google.api.ads.adwords.axis.v201809.rm.Member) MutateMembersReturnValue(com.google.api.ads.adwords.axis.v201809.rm.MutateMembersReturnValue)

Example 79 with ApiException

use of com.google.api.ads.adwords.axis.v201809.cm.ApiException in project googleads-java-lib by googleads.

the class UploadOfflineConversions method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param conversionName the name of the conversion tracker.
 * @param gClid the GCLID for the conversion.
 * @param conversionTime the date and time of the conversion.
 * @param conversionValue the value of the conversion.
 * @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, String conversionName, String gClid, String conversionTime, double conversionValue) throws RemoteException {
    // Get the OfflineConversionFeedService.
    OfflineConversionFeedServiceInterface offlineConversionFeedService = adWordsServices.get(session, OfflineConversionFeedServiceInterface.class);
    // Associate offline conversions with the existing named conversion tracker. If this tracker
    // was newly created, it may be a few hours before it can accept conversions.
    OfflineConversionFeed feed = new OfflineConversionFeed();
    feed.setConversionName(conversionName);
    feed.setConversionTime(conversionTime);
    feed.setConversionValue(conversionValue);
    feed.setGoogleClickId(gClid);
    OfflineConversionFeedOperation offlineConversionOperation = new OfflineConversionFeedOperation();
    offlineConversionOperation.setOperator(Operator.ADD);
    offlineConversionOperation.setOperand(feed);
    OfflineConversionFeedReturnValue offlineConversionReturnValue = offlineConversionFeedService.mutate(new OfflineConversionFeedOperation[] { offlineConversionOperation });
    OfflineConversionFeed newFeed = offlineConversionReturnValue.getValue(0);
    System.out.printf("Uploaded offline conversion value of %.4f for Google Click ID '%s' to '%s'.%n", newFeed.getConversionValue(), newFeed.getGoogleClickId(), newFeed.getConversionName());
}
Also used : OfflineConversionFeedReturnValue(com.google.api.ads.adwords.axis.v201809.cm.OfflineConversionFeedReturnValue) OfflineConversionFeed(com.google.api.ads.adwords.axis.v201809.cm.OfflineConversionFeed) OfflineConversionFeedOperation(com.google.api.ads.adwords.axis.v201809.cm.OfflineConversionFeedOperation) OfflineConversionFeedServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.OfflineConversionFeedServiceInterface)

Example 80 with ApiException

use of com.google.api.ads.adwords.axis.v201809.cm.ApiException in project googleads-java-lib by googleads.

the class AddProductScope 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();
    AddProductScopeParams params = new AddProductScopeParams();
    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");
    }
    try {
        runExample(adWordsServices, session, params.campaignId);
    } 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

ApiException (com.google.api.ads.adwords.axis.v201809.cm.ApiException)102 ApiError (com.google.api.ads.adwords.axis.v201809.cm.ApiError)100 OAuthException (com.google.api.ads.common.lib.exception.OAuthException)100 ValidationException (com.google.api.ads.common.lib.exception.ValidationException)100 RemoteException (java.rmi.RemoteException)100 ConfigurationLoadException (com.google.api.ads.common.lib.conf.ConfigurationLoadException)99 AdWordsServicesInterface (com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface)98 AdWordsSession (com.google.api.ads.adwords.lib.client.AdWordsSession)97 Credential (com.google.api.client.auth.oauth2.Credential)97 SelectorBuilder (com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)47 ArrayList (java.util.ArrayList)23 Selector (com.google.api.ads.adwords.axis.v201809.cm.Selector)21 Money (com.google.api.ads.adwords.axis.v201809.cm.Money)18 AdGroupAd (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd)17 AdGroupAdServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface)16 Campaign (com.google.api.ads.adwords.axis.v201809.cm.Campaign)15 CampaignServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.CampaignServiceInterface)14 Budget (com.google.api.ads.adwords.axis.v201809.cm.Budget)13 BiddingStrategyConfiguration (com.google.api.ads.adwords.axis.v201809.cm.BiddingStrategyConfiguration)12 IOException (java.io.IOException)12