From 58bd457592d33316108af041f5267e9caa154553 Mon Sep 17 00:00:00 2001 From: Jerjou Cheng Date: Thu, 13 Aug 2015 15:41:02 -0700 Subject: [PATCH] Move things around a bit for the doc. --- .../bigquery/samples/BigqueryServiceFactory.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/bigquery/src/main/java/com/google/cloud/bigquery/samples/BigqueryServiceFactory.java b/bigquery/src/main/java/com/google/cloud/bigquery/samples/BigqueryServiceFactory.java index 4a657935012..ad79ddb2721 100644 --- a/bigquery/src/main/java/com/google/cloud/bigquery/samples/BigqueryServiceFactory.java +++ b/bigquery/src/main/java/com/google/cloud/bigquery/samples/BigqueryServiceFactory.java @@ -16,6 +16,7 @@ package com.google.cloud.bigquery.samples; +// [START imports] import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.HttpTransport; import com.google.api.client.http.javanet.NetHttpTransport; @@ -23,6 +24,7 @@ import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.bigquery.Bigquery; import com.google.api.services.bigquery.BigqueryScopes; +// [END imports] import java.io.IOException; import java.util.Collection; @@ -67,21 +69,27 @@ public static Bigquery getService() throws IOException { /** * Creates an authorized client to Google Bigquery. + * * @return The BigQuery Service * @throws IOException Thrown if there is an error connecting */ // [START get_service] private static Bigquery createAuthorizedClient() throws IOException { - Collection bigqueryScopes = BigqueryScopes.all(); + // Create the credential HttpTransport transport = new NetHttpTransport(); JsonFactory jsonFactory = new JacksonFactory(); - GoogleCredential credential = GoogleCredential.getApplicationDefault( - transport, jsonFactory); + GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory); + + // Depending on the environment that provides the default credentials (eg Compute Engine, App + // Engine), the credentials may require us to specify the scopes we need explicitly. + // Check for this case, and inject the Bigquery scope if required. if (credential.createScopedRequired()) { + Collection bigqueryScopes = BigqueryScopes.all(); credential = credential.createScoped(bigqueryScopes); } + return new Bigquery.Builder(transport, jsonFactory, credential) - .setApplicationName("BigQuery Samples").build(); + .setApplicationName("BigQuery Samples").build(); } // [END get_service]