Skip to content

Commit a1b2b35

Browse files
committed
Use default application credential
1 parent 0dec937 commit a1b2b35

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

config/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Getting started
1515

1616
1. [Add Firebase to your Android Project](https://firebase.google.com/docs/android/setup).
1717
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.
18-
- Copy the private key JSON file to this folder and rename it to `service-account.json`.
18+
- Set `GOOGLE_APPLICATION_CREDENTIALS` environment variable to path of downloaded credential file.
1919
3. Change the `PROJECT_ID` variable in `Configure.java` to your project ID.
2020

2121
Run

config/src/main/java/com/google/firebase/samples/config/Configure.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)