diff --git a/samples/install-without-bom/pom.xml b/samples/install-without-bom/pom.xml
index 83ac12246..a22245325 100644
--- a/samples/install-without-bom/pom.xml
+++ b/samples/install-without-bom/pom.xml
@@ -49,6 +49,16 @@
+
+ com.google.oauth-client
+ google-oauth-client-java6
+ 1.30.6
+
+
+ com.google.oauth-client
+ google-oauth-client-jetty
+ 1.30.6
+
junit
diff --git a/samples/snapshot/pom.xml b/samples/snapshot/pom.xml
index 8f0c1a013..2964d37d8 100644
--- a/samples/snapshot/pom.xml
+++ b/samples/snapshot/pom.xml
@@ -47,7 +47,16 @@
1.116.7-SNAPSHOT
-
+
+ com.google.oauth-client
+ google-oauth-client-java6
+ 1.30.6
+
+
+ com.google.oauth-client
+ google-oauth-client-jetty
+ 1.30.6
+
junit
@@ -61,7 +70,6 @@
1.0.1
test
-
diff --git a/samples/snippets/pom.xml b/samples/snippets/pom.xml
index 69fd21cae..d5dc4a134 100644
--- a/samples/snippets/pom.xml
+++ b/samples/snippets/pom.xml
@@ -59,6 +59,16 @@
+
+ com.google.oauth-client
+ google-oauth-client-java6
+ 1.30.6
+
+
+ com.google.oauth-client
+ google-oauth-client-jetty
+ 1.30.6
+
junit
diff --git a/samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java b/samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java
new file mode 100644
index 000000000..4d2de24f4
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/bigquery/AuthUserFlow.java
@@ -0,0 +1,122 @@
+/*
+ * Copyright 2020 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.bigquery;
+
+// [START bigquery_auth_user_flow]
+import com.google.api.client.auth.oauth2.Credential;
+import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
+import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
+import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
+import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
+import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
+import com.google.api.client.json.JsonFactory;
+import com.google.api.client.json.jackson2.JacksonFactory;
+import com.google.api.client.util.store.FileDataStoreFactory;
+import com.google.api.gax.paging.Page;
+import com.google.auth.oauth2.GoogleCredentials;
+import com.google.auth.oauth2.UserCredentials;
+import com.google.cloud.bigquery.BigQuery;
+import com.google.cloud.bigquery.BigQueryException;
+import com.google.cloud.bigquery.BigQueryOptions;
+import com.google.cloud.bigquery.Dataset;
+import com.google.common.collect.ImmutableList;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.security.GeneralSecurityException;
+import java.util.List;
+
+// Sample to authenticate by using a user credential
+public class AuthUserFlow {
+
+ private static final File DATA_STORE_DIR =
+ new File(AuthUserFlow.class.getResource("/").getPath(), "credentials");
+ private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
+ // i.e redirect_uri http://localhost:61984/Callback
+ private static final int LOCAL_RECEIVER_PORT = 61984;
+
+ public static void runAuthUserFlow() {
+ // TODO(developer): Replace these variables before running the sample.
+ /**
+ * Download your OAuth2 configuration from the Google Developers Console API Credentials page.
+ * https://console.cloud.google.com/apis/credentials
+ */
+ Path credentialsPath = Paths.get("path/to/your/client_secret.json");
+ List scopes = ImmutableList.of("https://www.googleapis.com/auth/bigquery");
+ authUserFlow(credentialsPath, scopes);
+ }
+
+ public static void authUserFlow(Path credentialsPath, List selectedScopes) {
+ // Reading credentials file
+ try (InputStream inputStream = Files.newInputStream(credentialsPath)) {
+
+ // Load client_secret.json file
+ GoogleClientSecrets clientSecrets =
+ GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(inputStream));
+ String clientId = clientSecrets.getDetails().getClientId();
+ String clientSecret = clientSecrets.getDetails().getClientSecret();
+
+ // Generate the url that will be used for the consent dialog.
+ GoogleAuthorizationCodeFlow flow =
+ new GoogleAuthorizationCodeFlow.Builder(
+ GoogleNetHttpTransport.newTrustedTransport(),
+ JSON_FACTORY,
+ clientSecrets,
+ selectedScopes)
+ .setDataStoreFactory(new FileDataStoreFactory(DATA_STORE_DIR))
+ .setAccessType("offline")
+ .setApprovalPrompt("auto")
+ .build();
+
+ // Exchange an authorization code for refresh token
+ LocalServerReceiver receiver =
+ new LocalServerReceiver.Builder().setPort(LOCAL_RECEIVER_PORT).build();
+ Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
+
+ // OAuth2 Credentials representing a user's identity and consent
+ GoogleCredentials credentials =
+ UserCredentials.newBuilder()
+ .setClientId(clientId)
+ .setClientSecret(clientSecret)
+ .setRefreshToken(credential.getRefreshToken())
+ .build();
+
+ // Initialize client that will be used to send requests. This client only needs to be created
+ // once, and can be reused for multiple requests.
+ BigQuery bigquery =
+ BigQueryOptions.newBuilder().setCredentials(credentials).build().getService();
+
+ Page datasets = bigquery.listDatasets(BigQuery.DatasetListOption.pageSize(100));
+ if (datasets == null) {
+ System.out.println("Dataset does not contain any models");
+ return;
+ }
+ datasets
+ .iterateAll()
+ .forEach(
+ dataset -> System.out.printf("Success! Dataset ID: %s ", dataset.getDatasetId()));
+
+ } catch (BigQueryException | IOException | GeneralSecurityException ex) {
+ System.out.println("Project does not contain any datasets \n" + ex.toString());
+ }
+ }
+}
+// [END bigquery_auth_user_flow]
diff --git a/samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java b/samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java
new file mode 100644
index 000000000..7be558659
--- /dev/null
+++ b/samples/snippets/src/main/java/com/example/bigquery/AuthUserQuery.java
@@ -0,0 +1,128 @@
+/*
+ * Copyright 2020 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.bigquery;
+
+// [START bigquery_auth_user_query]
+import com.google.api.client.auth.oauth2.Credential;
+import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
+import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
+import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
+import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
+import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
+import com.google.api.client.json.JsonFactory;
+import com.google.api.client.json.jackson2.JacksonFactory;
+import com.google.api.client.util.store.FileDataStoreFactory;
+import com.google.auth.oauth2.GoogleCredentials;
+import com.google.auth.oauth2.UserCredentials;
+import com.google.cloud.bigquery.BigQuery;
+import com.google.cloud.bigquery.BigQueryException;
+import com.google.cloud.bigquery.BigQueryOptions;
+import com.google.cloud.bigquery.QueryJobConfiguration;
+import com.google.cloud.bigquery.TableResult;
+import com.google.common.collect.ImmutableList;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.security.GeneralSecurityException;
+import java.util.List;
+
+// Sample to query by using a user credential
+public class AuthUserQuery {
+
+ private static final File DATA_STORE_DIR =
+ new File(AuthUserQuery.class.getResource("/").getPath(), "credentials");
+ private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
+ // i.e redirect_uri http://localhost:61984/Callback
+ private static final int LOCAL_RECEIVER_PORT = 61984;
+
+ public static void runAuthUserQuery() {
+ // TODO(developer): Replace these variables before running the sample.
+ /**
+ * Download your OAuth2 configuration from the Google Developers Console API Credentials page.
+ * https://console.cloud.google.com/apis/credentials
+ */
+ Path credentialsPath = Paths.get("path/to/your/client_secret.json");
+ List scopes = ImmutableList.of("https://www.googleapis.com/auth/bigquery");
+ String query =
+ "SELECT name, SUM(number) as total"
+ + " FROM `bigquery-public-data.usa_names.usa_1910_current`"
+ + " WHERE name = 'William'"
+ + " GROUP BY name;";
+ authUserQuery(credentialsPath, scopes, query);
+ }
+
+ public static void authUserQuery(
+ Path credentialsPath, List selectedScopes, String query) {
+ // Reading credentials file
+ try (InputStream inputStream = Files.newInputStream(credentialsPath)) {
+
+ // Load client_secret.json file
+ GoogleClientSecrets clientSecrets =
+ GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(inputStream));
+ String clientId = clientSecrets.getDetails().getClientId();
+ String clientSecret = clientSecrets.getDetails().getClientSecret();
+
+ // Generate the url that will be used for the consent dialog.
+ GoogleAuthorizationCodeFlow flow =
+ new GoogleAuthorizationCodeFlow.Builder(
+ GoogleNetHttpTransport.newTrustedTransport(),
+ JSON_FACTORY,
+ clientSecrets,
+ selectedScopes)
+ .setDataStoreFactory(new FileDataStoreFactory(DATA_STORE_DIR))
+ .setAccessType("offline")
+ .setApprovalPrompt("auto")
+ .build();
+
+ // Exchange an authorization code for refresh token
+ LocalServerReceiver receiver =
+ new LocalServerReceiver.Builder().setPort(LOCAL_RECEIVER_PORT).build();
+ Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
+
+ // OAuth2 Credentials representing a user's identity and consent
+ GoogleCredentials credentials =
+ UserCredentials.newBuilder()
+ .setClientId(clientId)
+ .setClientSecret(clientSecret)
+ .setRefreshToken(credential.getRefreshToken())
+ .build();
+
+ // Initialize client that will be used to send requests. This client only needs to be created
+ // once, and can be reused for multiple requests.
+ BigQuery bigquery =
+ BigQueryOptions.newBuilder().setCredentials(credentials).build().getService();
+
+ QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder(query).build();
+
+ TableResult results = bigquery.query(queryConfig);
+
+ results
+ .iterateAll()
+ .forEach(row -> row.forEach(val -> System.out.printf("%s,", val.toString())));
+
+ System.out.println("Query performed successfully.");
+
+ } catch (BigQueryException | IOException | GeneralSecurityException | InterruptedException ex) {
+ System.out.println("Query not performed \n" + ex.toString());
+ }
+ }
+}
+// [END bigquery_auth_user_query]