Skip to content

Commit 352f259

Browse files
authored
Refresh the OAuth credentials proactively (#483)
This should help prevent hitting infinite retry loop when the credentials are invalid Change-Id: If07b968bfb6fb7ca81c76f0a68a2ac0333d1dae5
1 parent 0f4318e commit 352f259

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

google-ads/src/main/java/com/google/ads/googleads/lib/GoogleAdsClient.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.api.gax.rpc.TransportChannelProvider;
2323
import com.google.auth.Credentials;
2424
import com.google.auth.oauth2.GoogleCredentials;
25+
import com.google.auth.oauth2.OAuth2Credentials;
2526
import com.google.auth.oauth2.ServiceAccountCredentials;
2627
import com.google.auth.oauth2.UserCredentials;
2728
import com.google.auto.value.AutoValue;
@@ -548,6 +549,10 @@ public GoogleAdsClient build() {
548549
// action is needed.
549550
}
550551

552+
// Refreshes the OAuth credentials if necessary. This also ensures that the credentials are
553+
// valid, avoiding https://github.com/googleads/google-ads-java/issues/169
554+
refreshCredentialsIfNecessary();
555+
551556
// Provides the credentials to the primer to preemptively get these ready for usage.
552557
Primer.getInstance().ifPresent(p -> p.primeCredentialsAsync(getCredentials()));
553558
// Proceeds with creating the client library instance.
@@ -576,6 +581,19 @@ public GoogleAdsClient build() {
576581
return provider;
577582
}
578583

584+
/** Attempts to refresh the OAuth credentials if necessary. */
585+
private void refreshCredentialsIfNecessary() {
586+
Credentials credentials = getCredentials();
587+
if (credentials instanceof OAuth2Credentials) {
588+
OAuth2Credentials oAuth2Credentials = (OAuth2Credentials) credentials;
589+
try {
590+
oAuth2Credentials.refreshIfExpired();
591+
} catch (IOException e) {
592+
throw new OAuthException(e);
593+
}
594+
}
595+
}
596+
579597
@VisibleForTesting
580598
Builder setConfigurationFileSupplier(Supplier<File> configurationFileSupplier) {
581599
this.configurationFileSupplier = configurationFileSupplier;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.ads.googleads.lib;
16+
17+
import java.io.IOException;
18+
19+
/** Defines an exception that occured when configuring OAuth. */
20+
public class OAuthException extends RuntimeException {
21+
22+
public OAuthException(IOException cause) {
23+
super(cause);
24+
}
25+
}

0 commit comments

Comments
 (0)