Search in sources :

Example 1 with AdWordsSession

use of com.google.api.ads.adwords.lib.client.AdWordsSession in project googleads-java-lib by googleads.

the class AdHocReportDownloadHelperTest method testDownloadReportWithBadEndpoint_fails.

/**
 * Tests that the helper will throw a {@link ConnectException} wrapped in a
 * {@link ReportException} when the endpoint is for an unused port on localhost.
 */
@Test
public void testDownloadReportWithBadEndpoint_fails() throws Throwable {
    int port = TestPortFinder.getInstance().checkOutUnusedPort();
    try {
        // Construct the session to use an endpoint that is NOT in use on localhost.
        AdWordsSession sessionWithBadEndpoint = new AdWordsSession.Builder().withUserAgent("TEST_APP").withOAuth2Credential(credential).withEndpoint("https://localhost:" + port).withDeveloperToken("TEST_DEVELOPER_TOKEN").withClientCustomerId("TEST_CLIENT_CUSTOMER_ID").build();
        helper = new GenericAdWordsServices().getBootstrapper().getInstanceOf(sessionWithBadEndpoint, AdHocReportDownloadHelper.class);
        // Set up the ReportRequest so it will pass basic validation.
        when(reportRequest.getRequestType()).thenReturn(RequestType.AWQL);
        String awqlString = "SELECT BadField1 FROM NOT_A_REPORT DURING NOT_A_TIME_PERIOD";
        when(reportRequest.getReportRequestString()).thenReturn(awqlString);
        // The cause should be a ConnectException (see expected annotation) since the endpoint
        // port is not in use.
        thrown.expect(ReportException.class);
        thrown.expectCause(Matchers.<Exception>instanceOf(ConnectException.class));
        downloadReport();
    } finally {
        TestPortFinder.getInstance().releaseUnusedPort(port);
    }
}
Also used : GenericAdWordsServices(com.google.api.ads.adwords.lib.utils.testing.GenericAdWordsServices) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) Builder(com.google.api.ads.adwords.lib.utils.DetailedReportDownloadResponseException.Builder) Matchers.containsString(org.hamcrest.Matchers.containsString) ConnectException(java.net.ConnectException) MockHttpIntegrationTest(com.google.api.ads.common.lib.testing.MockHttpIntegrationTest) Test(org.junit.Test)

Example 2 with AdWordsSession

use of com.google.api.ads.adwords.lib.client.AdWordsSession in project googleads-java-lib by googleads.

the class AddCompleteCampaignsUsingBatchJob 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 (InterruptedException ie) {
        System.err.printf("Thread was interrupted: %s%n", ie);
    } catch (TimeoutException te) {
        System.err.printf("Job did not complete after status was polled %s times: %s%n", MAX_POLL_ATTEMPTS, te);
    } catch (BatchJobException be) {
        System.err.printf("Example failed due to BatchJobException: %s%n", be);
    }
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) ValidationException(com.google.api.ads.common.lib.exception.ValidationException) SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) OAuthException(com.google.api.ads.common.lib.exception.OAuthException) AdWordsServicesInterface(com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface) BatchJobException(com.google.api.ads.adwords.lib.utils.BatchJobException) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) ApiError(com.google.api.ads.adwords.axis.v201809.cm.ApiError) RemoteException(java.rmi.RemoteException) ApiException(com.google.api.ads.adwords.axis.v201809.cm.ApiException) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with AdWordsSession

use of com.google.api.ads.adwords.lib.client.AdWordsSession in project googleads-java-lib by googleads.

the class AddKeywordsUsingIncrementalBatchJob 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();
    AddKeywordsUsingIncrementalBatchJobParams params = new AddKeywordsUsingIncrementalBatchJobParams();
    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 (InterruptedException ie) {
        System.err.printf("Thread was interrupted: %s%n", ie);
    } catch (TimeoutException te) {
        System.err.printf("Job did not complete after status was polled %s times: %s%n", MAX_POLL_ATTEMPTS, te);
    } catch (BatchJobException be) {
        System.err.printf("Example failed due to BatchJobException: %s%n", be);
    }
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) ValidationException(com.google.api.ads.common.lib.exception.ValidationException) SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) OAuthException(com.google.api.ads.common.lib.exception.OAuthException) AdWordsServicesInterface(com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface) BatchJobException(com.google.api.ads.adwords.lib.utils.BatchJobException) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) ApiError(com.google.api.ads.adwords.axis.v201809.cm.ApiError) RemoteException(java.rmi.RemoteException) ApiException(com.google.api.ads.adwords.axis.v201809.cm.ApiException) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with AdWordsSession

use of com.google.api.ads.adwords.lib.client.AdWordsSession in project googleads-java-lib by googleads.

the class GetAllDisapprovedAds 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();
    GetAllDisapprovedAdsParams params = new GetAllDisapprovedAdsParams();
    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);
    }
}
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 5 with AdWordsSession

use of com.google.api.ads.adwords.lib.client.AdWordsSession in project googleads-java-lib by googleads.

the class GetCampaignsByLabel 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();
    GetCampaignsByLabelParams params = new GetCampaignsByLabelParams();
    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.labelId = Long.parseLong("INSERT_LABEL_ID_HERE");
    }
    try {
        runExample(adWordsServices, session, params.labelId);
    } 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)

Aggregations

AdWordsSession (com.google.api.ads.adwords.lib.client.AdWordsSession)121 Credential (com.google.api.client.auth.oauth2.Credential)104 AdWordsServicesInterface (com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface)102 ValidationException (com.google.api.ads.common.lib.exception.ValidationException)101 ConfigurationLoadException (com.google.api.ads.common.lib.conf.ConfigurationLoadException)100 OAuthException (com.google.api.ads.common.lib.exception.OAuthException)100 ApiError (com.google.api.ads.adwords.axis.v201809.cm.ApiError)97 ApiException (com.google.api.ads.adwords.axis.v201809.cm.ApiException)97 RemoteException (java.rmi.RemoteException)97 SelectorBuilder (com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)24 Test (org.junit.Test)15 IOException (java.io.IOException)14 OfflineCredentials (com.google.api.ads.common.lib.auth.OfflineCredentials)12 AdWordsServices (com.google.api.ads.adwords.axis.factory.AdWordsServices)10 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 MockHttpIntegrationTest (com.google.api.ads.common.lib.testing.MockHttpIntegrationTest)8 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)7 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)7 List (java.util.List)7