Skip to content

Commit

Permalink
Move things around a bit for the doc.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerjou Cheng committed Aug 14, 2015
1 parent bc1c128 commit 58bd457
Showing 1 changed file with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

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;
import com.google.api.client.json.JsonFactory;
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;
Expand Down Expand Up @@ -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<String> 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<String> 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]

Expand Down

0 comments on commit 58bd457

Please sign in to comment.