Search in sources :

Example 96 with AdWordsSession

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

the class AdWordsServiceClientFactoryHelperTest method testCheckServiceClientPreconditions_passOAuth2.

@Test
public void testCheckServiceClientPreconditions_passOAuth2() throws Exception {
    AdWordsServiceClientFactoryHelper helper = new AdWordsServiceClientFactoryHelper(adsServiceClientFactory, adsServiceDescriptorFactory, soapClientHandler, adsLibConfiguration);
    Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod());
    AdWordsSession adWordsSession = new AdWordsSession.Builder().withUserAgent("FooBar").withEndpoint("https://www.google.com").withOAuth2Credential(credential).withDeveloperToken("developerToken").build();
    helper.checkServiceClientPreconditions(adWordsSession, com.google.api.ads.adwords.lib.factory.helper.testing.v201406.cm.TestService.class);
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) Test(org.junit.Test)

Example 97 with AdWordsSession

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

the class AdWordsJaxWsHeaderHandlerTest method testSetAuthenticationHeaders.

@Test
public void testSetAuthenticationHeaders() throws Exception {
    Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod());
    Object soapClient = new Object();
    Map<String, Object> headerElements = new HashMap<String, Object>();
    AdWordsSession adWordsSession = new AdWordsSession.Builder().withOAuth2Credential(credential).withDeveloperToken("developerToken").withUserAgent(USER_AGENT).build();
    headerHandler.setAuthenticationHeaders(soapClient, headerElements, adWordsSession);
    verify(authorizationHeaderHandler).setAuthorization(soapClient, adWordsSession);
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) HashMap(java.util.HashMap) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) Test(org.junit.Test)

Example 98 with AdWordsSession

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

the class AdWordsJaxWsSoapIntegrationTest method testGoldenSoap_oauth2.

/**
 * Tests making a JAX-WS Ad Manager API call with OAuth2.
 */
@Test
public void testGoldenSoap_oauth2() throws Exception {
    testHttpServer.setMockResponseBody(SoapResponseXmlProvider.getTestSoapResponse(API_VERSION));
    GoogleCredential credential = new GoogleCredential.Builder().setTransport(new NetHttpTransport()).setJsonFactory(new JacksonFactory()).build();
    credential.setAccessToken("TEST_ACCESS_TOKEN");
    AdWordsSession session = new AdWordsSession.Builder().withUserAgent("TEST_APP").withOAuth2Credential(credential).withEndpoint(testHttpServer.getServerUrl()).withDeveloperToken("TEST_DEVELOPER_TOKEN").withClientCustomerId("TEST_CLIENT_CUSTOMER_ID").build();
    BudgetServiceInterface budgetService = new AdWordsServices().get(session, BudgetServiceInterface.class);
    Budget budget = new Budget();
    budget.setName("Test Budget Name");
    Money money = new Money();
    money.setMicroAmount(50000000L);
    budget.setAmount(money);
    budget.setDeliveryMethod(BudgetBudgetDeliveryMethod.STANDARD);
    BudgetOperation operation = new BudgetOperation();
    operation.setOperand(budget);
    operation.setOperator(Operator.ADD);
    Budget responseBudget = budgetService.mutate(Lists.newArrayList(operation)).getValue().get(0);
    assertEquals("Budget ID does not match", 251877074L, responseBudget.getBudgetId().longValue());
    assertEquals("Budget name does not match", budget.getName(), responseBudget.getName());
    assertEquals("Budget amount does not match", budget.getAmount().getMicroAmount(), responseBudget.getAmount().getMicroAmount());
    assertEquals("Budget delivery method does not match", budget.getDeliveryMethod(), responseBudget.getDeliveryMethod());
    Diff diff = DiffBuilder.compare(SoapRequestXmlProvider.getOAuth2SoapRequest(API_VERSION)).withTest(testHttpServer.getLastRequestBody()).checkForSimilar().build();
    assertFalse(diff.hasDifferences());
    assertFalse("Did not request compression but request was compressed", testHttpServer.wasLastRequestBodyCompressed());
    assertEquals("Bearer TEST_ACCESS_TOKEN", testHttpServer.getLastAuthorizationHttpHeader());
}
Also used : Money(com.google.api.ads.adwords.jaxws.v201809.cm.Money) Diff(org.xmlunit.diff.Diff) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) DiffBuilder(org.xmlunit.builder.DiffBuilder) BudgetOperation(com.google.api.ads.adwords.jaxws.v201809.cm.BudgetOperation) BudgetServiceInterface(com.google.api.ads.adwords.jaxws.v201809.cm.BudgetServiceInterface) Budget(com.google.api.ads.adwords.jaxws.v201809.cm.Budget) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) AdWordsServices(com.google.api.ads.adwords.jaxws.factory.AdWordsServices) MockHttpIntegrationTest(com.google.api.ads.common.lib.testing.MockHttpIntegrationTest) Test(org.junit.Test)

Example 99 with AdWordsSession

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

the class AdWordsJaxWsSoapTimeoutIntegrationTest method testRequestTimeoutEnforced.

/**
 * Tests that the request timeout in ads.properties is enforced.
 */
@Test
public void testRequestTimeoutEnforced() throws Exception {
    testHttpServer.setMockResponseBody(SoapResponseXmlProvider.getTestSoapResponse(API_VERSION));
    testHttpServer.setDelay(200L);
    GoogleCredential credential = new GoogleCredential.Builder().setTransport(new NetHttpTransport()).setJsonFactory(new JacksonFactory()).build();
    credential.setAccessToken("TEST_ACCESS_TOKEN");
    AdWordsSession session = new AdWordsSession.Builder().withUserAgent("TEST_APP").withOAuth2Credential(credential).withEndpoint(testHttpServer.getServerUrl()).withDeveloperToken("TEST_DEVELOPER_TOKEN").withClientCustomerId("TEST_CLIENT_CUSTOMER_ID").build();
    BudgetServiceInterface budgetService = new AdWordsServices().get(session, BudgetServiceInterface.class);
    Budget budget = new Budget();
    budget.setName("Test Budget Name");
    Money money = new Money();
    money.setMicroAmount(50000000L);
    budget.setAmount(money);
    budget.setDeliveryMethod(BudgetBudgetDeliveryMethod.STANDARD);
    BudgetOperation operation = new BudgetOperation();
    operation.setOperand(budget);
    operation.setOperator(Operator.ADD);
    thrown.expect(WebServiceException.class);
    thrown.expectMessage("Read timed out");
    budgetService.mutate(Lists.newArrayList(operation));
}
Also used : Money(com.google.api.ads.adwords.jaxws.v201809.cm.Money) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) BudgetOperation(com.google.api.ads.adwords.jaxws.v201809.cm.BudgetOperation) BudgetServiceInterface(com.google.api.ads.adwords.jaxws.v201809.cm.BudgetServiceInterface) Budget(com.google.api.ads.adwords.jaxws.v201809.cm.Budget) GoogleCredential(com.google.api.client.googleapis.auth.oauth2.GoogleCredential) JacksonFactory(com.google.api.client.json.jackson2.JacksonFactory) AdWordsServices(com.google.api.ads.adwords.jaxws.factory.AdWordsServices) Test(org.junit.Test) MockHttpIntegrationTest(com.google.api.ads.common.lib.testing.MockHttpIntegrationTest)

Example 100 with AdWordsSession

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

the class GetResponsiveSearchAds 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();
    GetResponsiveSearchAdsParams params = new GetResponsiveSearchAdsParams();
    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)

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