From a1b2b3555747cbf2aea57390b34c3225bff28f5a Mon Sep 17 00:00:00 2001 From: Arthur Thompson Date: Thu, 24 Jan 2019 16:31:59 -0800 Subject: [PATCH 1/3] Use default application credential --- config/README.md | 2 +- .../google/firebase/samples/config/Configure.java | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/config/README.md b/config/README.md index 070dcc8..111fc6e 100644 --- a/config/README.md +++ b/config/README.md @@ -15,7 +15,7 @@ Getting started 1. [Add Firebase to your Android Project](https://firebase.google.com/docs/android/setup). 2. Create a service account as described in [Adding Firebase to your Server](https://firebase.google.com/docs/admin/setup) and download the JSON file. - - Copy the private key JSON file to this folder and rename it to `service-account.json`. + - Set `GOOGLE_APPLICATION_CREDENTIALS` environment variable to path of downloaded credential file. 3. Change the `PROJECT_ID` variable in `Configure.java` to your project ID. Run diff --git a/config/src/main/java/com/google/firebase/samples/config/Configure.java b/config/src/main/java/com/google/firebase/samples/config/Configure.java index fe79e15..1965017 100644 --- a/config/src/main/java/com/google/firebase/samples/config/Configure.java +++ b/config/src/main/java/com/google/firebase/samples/config/Configure.java @@ -37,15 +37,20 @@ public class Configure { * Retrieve a valid access token that can be use to authorize requests to the Remote Config REST * API. * + * This method assumes it is either running in a trusted Google environment like GCP or if running + * elsewhere the the GOOGLE_APPLICATION_CREDENTIALS environment variable is set to the path of the + * service account credentials file. + * * @return Access token. * @throws IOException */ // [START retrieve_access_token] private static String getAccessToken() throws IOException { - GoogleCredential googleCredential = GoogleCredential - .fromStream(new FileInputStream("service-account.json")) + GoogleCredential googleCredential = GoogleCredential.getApplicationDefault() .createScoped(Arrays.asList(SCOPES)); - googleCredential.refreshToken(); + if (googleCredential.getAccessToken() == null || googleCredential.getExpiresInSeconds() < 300) { + googleCredential.refreshToken(); + } return googleCredential.getAccessToken(); } // [END retrieve_access_token] @@ -83,7 +88,9 @@ private static void getTemplate() throws IOException { String etag = httpURLConnection.getHeaderField("ETag"); System.out.println("ETag from server: " + etag); } else { - System.out.println(inputstreamToString(httpURLConnection.getErrorStream())); + System.out.println("Error:"); + InputStream inputStream = new GZIPInputStream(httpURLConnection.getErrorStream()); + System.out.println(inputstreamToString(inputStream)); } } From 01c89d213ff3ad4cb4dd039ab256bd2d7d1ceb31 Mon Sep 17 00:00:00 2001 From: Arthur Thompson Date: Wed, 30 Jan 2019 14:01:30 -0800 Subject: [PATCH 2/3] Update docs for getAccessToken --- .../java/com/google/firebase/samples/config/Configure.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/config/src/main/java/com/google/firebase/samples/config/Configure.java b/config/src/main/java/com/google/firebase/samples/config/Configure.java index 1965017..392fabe 100644 --- a/config/src/main/java/com/google/firebase/samples/config/Configure.java +++ b/config/src/main/java/com/google/firebase/samples/config/Configure.java @@ -37,9 +37,9 @@ public class Configure { * Retrieve a valid access token that can be use to authorize requests to the Remote Config REST * API. * - * This method assumes it is either running in a trusted Google environment like GCP or if running - * elsewhere the the GOOGLE_APPLICATION_CREDENTIALS environment variable is set to the path of the - * service account credentials file. + * This method must be called in either a trusted Google environment like GCP or if running + * elsewhere the GOOGLE_APPLICATION_CREDENTIALS environment variable must be set to the path + * of the service account credentials file. * * @return Access token. * @throws IOException From 82bc562fa7cc4bd97d2a6891ccde4413c36379bf Mon Sep 17 00:00:00 2001 From: Arthur Thompson Date: Wed, 30 Jan 2019 14:04:28 -0800 Subject: [PATCH 3/3] Explain token refresh strategy --- .../java/com/google/firebase/samples/config/Configure.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/src/main/java/com/google/firebase/samples/config/Configure.java b/config/src/main/java/com/google/firebase/samples/config/Configure.java index 392fabe..851bbca 100644 --- a/config/src/main/java/com/google/firebase/samples/config/Configure.java +++ b/config/src/main/java/com/google/firebase/samples/config/Configure.java @@ -35,7 +35,8 @@ public class Configure { /** * Retrieve a valid access token that can be use to authorize requests to the Remote Config REST - * API. + * API. If there is no existing access token or if the existing one will expire in less than + * five minutes a fresh token is fetched. * * This method must be called in either a trusted Google environment like GCP or if running * elsewhere the GOOGLE_APPLICATION_CREDENTIALS environment variable must be set to the path