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);
}
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);
}
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());
}
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));
}
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);
}
}
Aggregations