Important: Upgrade from Google Ads API v4 and v5 by June 23, 2021Dear Google Ads API Developer (Manager Account ID XXX),Google Ads API v4 and v5 have been deprecated and will stop accepting requests starting on June 23, 2021.Your developer token has recently submitted requests to v4 or v5. Migrate to the latest version as soon as possible to continue accessing the Google Ads API. Use the migration guide to upgrade.If you have any questions, please don't hesitate to contact us at googleadsapi-support@google.com.Sincerely,The Google Ads API Team
private Stream<AdsCustomer> fetchAdsCustomer(String customerId, String refreshToken) {
GoogleAdsClient googleAdsClient = getClient(customerId, refreshToken);
try (GoogleAdsServiceClient googleAdsServiceClient =
googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) {
String query =
"SELECT customer_client.id, customer_client.descriptive_name, "
+ "customer_client.currency_code, customer_client.time_zone, "
+ "customer_client.manager "
+ "FROM customer_client "
+ "WHERE customer_client.level <= 1";
SearchGoogleAdsStreamRequest request =
SearchGoogleAdsStreamRequest.newBuilder()
.setCustomerId(customerId)
.setQuery(query)
.build();
return getAdsCustomerFromStream(googleAdsServiceClient, request, customerId).stream();
} catch (Exception e) {
log.error("error while parsing google-ads exception", e);
return null;
}
}
private List<AdsCustomer> getAdsCustomerFromStream(
GoogleAdsServiceClient googleAdsServiceClient,
SearchGoogleAdsStreamRequest request,
String customerId
) {
Callable<List<AdsCustomer>> receiveResponse = () -> {
ServerStream<SearchGoogleAdsStreamResponse> stream =
googleAdsServiceClient.searchStreamCallable().call(request);
List<AdsCustomer> results = new ArrayList<>();
for (SearchGoogleAdsStreamResponse response : stream) {
response.getResultsList()
.forEach(googleAdsRow -> results.add(mapAdsCustomer(customerId, googleAdsRow)));
}
return results;
};
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<List<AdsCustomer>> future = executor.submit(receiveResponse);
try {
List<AdsCustomer> results = future.get(5000, TimeUnit.MILLISECONDS);
log.info("Successfully got AdsCustomer from stream for {} (the request was successful)", customerId);
return results;
} catch (TimeoutException e) {
log.warn("Failed to get AdsCustomer from stream for {} - the token probably didn't have the right permissions. {}"
, customerId, e.getMessage());
future.cancel(true);
} catch (InterruptedException | ExecutionException e) {
log.warn("Failed to get AdsCustomer from stream for {} (the thread failed). {}", customerId, e.getMessage());
} finally {
executor.shutdown();
}
return ImmutableList.of();
}