@@ -37,15 +37,20 @@ public class Configure {
3737 * Retrieve a valid access token that can be use to authorize requests to the Remote Config REST
3838 * API.
3939 *
40+ * This method assumes it is either running in a trusted Google environment like GCP or if running
41+ * elsewhere the the GOOGLE_APPLICATION_CREDENTIALS environment variable is set to the path of the
42+ * service account credentials file.
43+ *
4044 * @return Access token.
4145 * @throws IOException
4246 */
4347 // [START retrieve_access_token]
4448 private static String getAccessToken () throws IOException {
45- GoogleCredential googleCredential = GoogleCredential
46- .fromStream (new FileInputStream ("service-account.json" ))
49+ GoogleCredential googleCredential = GoogleCredential .getApplicationDefault ()
4750 .createScoped (Arrays .asList (SCOPES ));
48- googleCredential .refreshToken ();
51+ if (googleCredential .getAccessToken () == null || googleCredential .getExpiresInSeconds () < 300 ) {
52+ googleCredential .refreshToken ();
53+ }
4954 return googleCredential .getAccessToken ();
5055 }
5156 // [END retrieve_access_token]
@@ -83,7 +88,9 @@ private static void getTemplate() throws IOException {
8388 String etag = httpURLConnection .getHeaderField ("ETag" );
8489 System .out .println ("ETag from server: " + etag );
8590 } else {
86- System .out .println (inputstreamToString (httpURLConnection .getErrorStream ()));
91+ System .out .println ("Error:" );
92+ InputStream inputStream = new GZIPInputStream (httpURLConnection .getErrorStream ());
93+ System .out .println (inputstreamToString (inputStream ));
8794 }
8895
8996 }
0 commit comments