diff --git a/google-ads/src/main/java/com/google/ads/googleads/lib/GoogleAdsClient.java b/google-ads/src/main/java/com/google/ads/googleads/lib/GoogleAdsClient.java index 520e59e964..9baec92f56 100644 --- a/google-ads/src/main/java/com/google/ads/googleads/lib/GoogleAdsClient.java +++ b/google-ads/src/main/java/com/google/ads/googleads/lib/GoogleAdsClient.java @@ -106,8 +106,7 @@ public static Builder newBuilder() { clientBuilder .setEndpoint(DEFAULT_ENDPOINT) .setTransportChannelProvider(transportChannelProvider) - .setDefaultTransportChannelProvider(transportChannelProvider) - .setUseCloudOrgForApiAccess(false); + .setDefaultTransportChannelProvider(transportChannelProvider); return clientBuilder; } @@ -124,12 +123,6 @@ public static Builder newBuilder() { @Nullable public abstract String getDeveloperToken(); - /** - * Whether to use the Google Cloud Organization of your Google Cloud project instead of developer - * token to determine your Google Ads API access level. - */ - abstract boolean isUseCloudOrgForApiAccess(); - /** Returns the endpoint to use. Defaults to DEFAULT_ENDPOINT. */ public abstract String getEndpoint(); @@ -407,10 +400,11 @@ abstract Builder setDefaultTransportChannelProvider( TransportChannelProvider transportChannelProvider); /** Returns the developer token currently configured. */ + @Nullable public abstract String getDeveloperToken(); /** Sets the developer token used to obtain access to the Google Ads API. */ - public abstract Builder setDeveloperToken(String developerToken); + public abstract Builder setDeveloperToken(@Nullable String developerToken); /** * Sets the developer token from the specified {@code properties} if an entry for developer @@ -423,18 +417,6 @@ private void setDeveloperToken(Properties properties) { } } - /** - * Gets whether to use the Google Cloud Organization of your Google Cloud project instead of - * developer token to determine your Google Ads API access level. - */ - public abstract boolean isUseCloudOrgForApiAccess(); - - /** - * Specifies whether to use the Google Cloud Organization of your Google Cloud project instead - * of developer token to determine your Google Ads API access level. - */ - public abstract Builder setUseCloudOrgForApiAccess(boolean useCloudOrgForApiAccess); - /** Returns the login customer ID currently configured. */ public abstract Long getLoginCustomerId(); @@ -657,24 +639,6 @@ public GoogleAdsClient build() { // action is needed. } - // Verifies that the client will use exactly one of dev token or Cloud org for API access. - final String errorSuffix = - "You must set either the developer token or set use Cloud org for API access to true, but" - + " not both."; - if (getDeveloperToken() == null) { - // If dev token is null, verifies that the client is using Cloud org for API access. - Preconditions.checkState( - isUseCloudOrgForApiAccess(), - "Developer token is null but not using cloud org for API access. %s", - errorSuffix); - } else { - // If dev token is not null, verifies that the client is not using Cloud org for API access. - Preconditions.checkState( - !isUseCloudOrgForApiAccess(), - "Developer token is not null but using Cloud org for API access. %s", - errorSuffix); - } - // Provides the credentials to the primer to preemptively get these ready for usage. Primer.getInstance().ifPresent(p -> p.primeCredentialsAsync(getCredentials())); // Proceeds with creating the client library instance. diff --git a/google-ads/src/main/java/com/google/ads/googleads/lib/GoogleAdsHeaderProvider.java b/google-ads/src/main/java/com/google/ads/googleads/lib/GoogleAdsHeaderProvider.java index cc0ea5c6ef..8a0fb7126c 100644 --- a/google-ads/src/main/java/com/google/ads/googleads/lib/GoogleAdsHeaderProvider.java +++ b/google-ads/src/main/java/com/google/ads/googleads/lib/GoogleAdsHeaderProvider.java @@ -22,6 +22,7 @@ import com.google.auto.value.AutoValue; import com.google.auto.value.extension.memoized.Memoized; import com.google.common.base.Joiner; +import com.google.common.base.Strings; import com.google.common.collect.ImmutableMap; import com.google.protobuf.Message; import java.io.IOException; @@ -64,7 +65,7 @@ public static Builder newBuilder() { @Memoized public ImmutableMap getHeaders() { Map headers = new HashMap<>(); - if (getDeveloperToken() != null) { + if (!Strings.isNullOrEmpty(getDeveloperToken())) { headers.put("developer-token", getDeveloperToken()); } if (getLoginCustomerId() != null) { @@ -134,7 +135,7 @@ private static String getProtobufVersion() { public abstract static class Builder { /** Sets the developer token. */ - public abstract Builder setDeveloperToken(String developerToken); + public abstract Builder setDeveloperToken(@Nullable String developerToken); /** Sets the login customer ID. */ public abstract Builder setLoginCustomerId(Long loginCustomerId); diff --git a/google-ads/src/test/java/com/google/ads/googleads/lib/GoogleAdsClientTest.java b/google-ads/src/test/java/com/google/ads/googleads/lib/GoogleAdsClientTest.java index 053ba6c971..22e9b79dbb 100644 --- a/google-ads/src/test/java/com/google/ads/googleads/lib/GoogleAdsClientTest.java +++ b/google-ads/src/test/java/com/google/ads/googleads/lib/GoogleAdsClientTest.java @@ -179,8 +179,7 @@ public void buildFromPropertiesFile_readsAllProperties() throws IOException { * token property. */ @Test - public void testBuildFromPropertiesFile_withoutDeveloperToken_withUseCloudOrgForApiAccess() - throws IOException { + public void testBuildFromPropertiesFile_withoutDeveloperToken() throws IOException { // Create a properties file in the temporary folder. File propertiesFile = folder.newFile("ads.properties"); // Remove the developer token property. @@ -190,41 +189,42 @@ public void testBuildFromPropertiesFile_withoutDeveloperToken_withUseCloudOrgFor } // Build a new client from the file. + GoogleAdsClient client = + GoogleAdsClient.newBuilder().fromPropertiesFile(propertiesFile).build(); + assertGoogleAdsClient(client, LOGIN_CUSTOMER_ID, true); + assertNull(client.getDeveloperToken()); + } + + /** Tests building a client without a developer token. */ + @Test + public void testBuild_withoutDeveloperToken() { GoogleAdsClient client = GoogleAdsClient.newBuilder() - .fromPropertiesFile(propertiesFile) - .setUseCloudOrgForApiAccess(true) + .fromProperties(testProperties) + .setDeveloperToken(null) .build(); - assertGoogleAdsClient(client, LOGIN_CUSTOMER_ID, true); + assertGoogleAdsClient(client, true); + assertNull(client.getDeveloperToken()); } - /** Tests that clients can only use exactly one of dev token or Cloud org for API access. */ + /** Tests that developer token is not included in headers when it is null. */ @Test - public void testDevTokenAndUseCloudOrgForApiAccessAreExclusive_failIfBothOrNeither() { - GoogleAdsClient.Builder builder = GoogleAdsClient.newBuilder().fromProperties(testProperties); - // Confirms developer token is set on the builder. - assertNotNull("dev token not set from test properties", builder.getDeveloperToken()); - // Opts into using Cloud org for API access. - builder.setUseCloudOrgForApiAccess(true); - // Confirms build() fails. - Throwable exception = - assertThrows( - "Should fail when both dev token is set and using Cloud org for API access", - IllegalStateException.class, - () -> builder.build()); - // Checks the exception message. - assertThat(exception.getMessage(), Matchers.containsString("not both")); - - // Clears dev token and opts out of using Cloud org for API access. - builder.setDeveloperToken(null).setUseCloudOrgForApiAccess(false); - // Confirms build() fails. - exception = - assertThrows( - "Should fail when neither dev token is set nor using Cloud org for API access", - IllegalStateException.class, - () -> builder.build()); - // Checks the exception message. - assertThat(exception.getMessage(), Matchers.containsString("not both")); + public void testNullDeveloperTokenNotAppearInHeaders() { + GoogleAdsClient.Builder builder = + GoogleAdsClient.newBuilder() + .fromProperties(testProperties) + .setDeveloperToken(null); + assertFalse(builder.getHeaders().containsKey("developer-token")); + } + + /** Tests that developer token is not included in headers when it is empty. */ + @Test + public void testEmptyDeveloperTokenNotAppearInHeaders() { + GoogleAdsClient.Builder builder = + GoogleAdsClient.newBuilder() + .fromProperties(testProperties) + .setDeveloperToken(""); + assertFalse(builder.getHeaders().containsKey("developer-token")); } /** @@ -1063,10 +1063,7 @@ private void assertGoogleAdsClient( channelProvider.toBuilder().getMaxInboundMessageSize()); } - if (client.getDeveloperToken() == null) { - assertTrue( - "Developer token is null but use cloud org is false", client.isUseCloudOrgForApiAccess()); - } else { + if (client.getDeveloperToken() != null) { assertEquals("developer token", DEVELOPER_TOKEN, client.getDeveloperToken()); } assertEquals("Login customer id", loginCustomerId, client.getLoginCustomerId()); diff --git a/google-ads/src/test/java/com/google/ads/googleads/lib/GoogleAdsHeaderProviderTest.java b/google-ads/src/test/java/com/google/ads/googleads/lib/GoogleAdsHeaderProviderTest.java index 9ea4b9fb11..f00363e547 100644 --- a/google-ads/src/test/java/com/google/ads/googleads/lib/GoogleAdsHeaderProviderTest.java +++ b/google-ads/src/test/java/com/google/ads/googleads/lib/GoogleAdsHeaderProviderTest.java @@ -37,6 +37,15 @@ public void developerTokenOptional() { assertFalse(provider.getHeaders().containsKey("developer-token")); } + /** Verifies that the developer token is not present in headers when empty. */ + @Test + public void developerTokenEmpty_notIncluded() { + GoogleAdsHeaderProvider provider = + GoogleAdsHeaderProvider.newBuilder().setDeveloperToken("").build(); + assertEquals("", provider.getDeveloperToken()); + assertFalse(provider.getHeaders().containsKey("developer-token")); + } + /** Verifies that the developer token is set and present in the headers when provided. */ @Test public void developerToken_includesIfSet() {