Skip to content

Commit 58bd457

Browse files
author
Jerjou Cheng
committed
Move things around a bit for the doc.
1 parent bc1c128 commit 58bd457

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

bigquery/src/main/java/com/google/cloud/bigquery/samples/BigqueryServiceFactory.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616

1717
package com.google.cloud.bigquery.samples;
1818

19+
// [START imports]
1920
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
2021
import com.google.api.client.http.HttpTransport;
2122
import com.google.api.client.http.javanet.NetHttpTransport;
2223
import com.google.api.client.json.JsonFactory;
2324
import com.google.api.client.json.jackson2.JacksonFactory;
2425
import com.google.api.services.bigquery.Bigquery;
2526
import com.google.api.services.bigquery.BigqueryScopes;
27+
// [END imports]
2628

2729
import java.io.IOException;
2830
import java.util.Collection;
@@ -67,21 +69,27 @@ public static Bigquery getService() throws IOException {
6769

6870
/**
6971
* Creates an authorized client to Google Bigquery.
72+
*
7073
* @return The BigQuery Service
7174
* @throws IOException Thrown if there is an error connecting
7275
*/
7376
// [START get_service]
7477
private static Bigquery createAuthorizedClient() throws IOException {
75-
Collection<String> bigqueryScopes = BigqueryScopes.all();
78+
// Create the credential
7679
HttpTransport transport = new NetHttpTransport();
7780
JsonFactory jsonFactory = new JacksonFactory();
78-
GoogleCredential credential = GoogleCredential.getApplicationDefault(
79-
transport, jsonFactory);
81+
GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory);
82+
83+
// Depending on the environment that provides the default credentials (eg Compute Engine, App
84+
// Engine), the credentials may require us to specify the scopes we need explicitly.
85+
// Check for this case, and inject the Bigquery scope if required.
8086
if (credential.createScopedRequired()) {
87+
Collection<String> bigqueryScopes = BigqueryScopes.all();
8188
credential = credential.createScoped(bigqueryScopes);
8289
}
90+
8391
return new Bigquery.Builder(transport, jsonFactory, credential)
84-
.setApplicationName("BigQuery Samples").build();
92+
.setApplicationName("BigQuery Samples").build();
8593
}
8694
// [END get_service]
8795

0 commit comments

Comments
 (0)