Skip to content

Commit 9fc83d8

Browse files
authored
Merge pull request GoogleCloudPlatform#758 from GoogleCloudPlatform/tswast-bq
BigQuery: Add explicit service account key auth sample.
2 parents f081bf5 + 82d2435 commit 9fc83d8

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

bigquery/cloud-client/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
service_account.json
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
Copyright 2017, Google, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
import com.google.auth.oauth2.GoogleCredentials;
20+
import com.google.auth.oauth2.ServiceAccountCredentials;
21+
import com.google.cloud.bigquery.BigQuery;
22+
import com.google.cloud.bigquery.BigQueryOptions;
23+
import com.google.cloud.bigquery.Dataset;
24+
25+
import java.io.File;
26+
import java.io.FileInputStream;
27+
import java.io.IOException;
28+
29+
/**
30+
* Examples for authenticating to Google BigQuery.
31+
*
32+
* <p>See: https://cloud.google.com/bigquery/authentication
33+
*/
34+
public class AuthSnippets {
35+
36+
// [START default_credentials]
37+
public static void implicit() {
38+
// Instantiate a client. If you don't specify credentials when constructing a client, the
39+
// client library will look for credentials in the environment, such as the
40+
// GOOGLE_APPLICATION_CREDENTIALS environment variable.
41+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
42+
43+
// Use the client.
44+
System.out.println("Datasets:");
45+
for (Dataset dataset : bigquery.listDatasets().iterateAll()) {
46+
System.out.printf("%s%n", dataset.getDatasetId().getDataset());
47+
}
48+
}
49+
// [END default_credentials]
50+
51+
// [START explicit_service_account]
52+
public static void explicit() throws IOException {
53+
// Load credentials from JSON key file. If you can't set the GOOGLE_APPLICATION_CREDENTIALS
54+
// environment variable, you can explicitly load the credentials file to construct the
55+
// credentials.
56+
GoogleCredentials credentials;
57+
File credentialsPath = new File("service_account.json"); // TODO: update to your key path.
58+
try (FileInputStream serviceAccountStream = new FileInputStream(credentialsPath)) {
59+
credentials = ServiceAccountCredentials.fromStream(serviceAccountStream);
60+
}
61+
62+
// Instantiate a client.
63+
BigQuery bigquery =
64+
BigQueryOptions.newBuilder().setCredentials(credentials).build().getService();
65+
66+
// Use the client.
67+
System.out.println("Datasets:");
68+
for (Dataset dataset : bigquery.listDatasets().iterateAll()) {
69+
System.out.printf("%s%n", dataset.getDatasetId().getDataset());
70+
}
71+
}
72+
// [END explicit_service_account]
73+
74+
public static void main(String... args) throws IOException {
75+
boolean validArgs = args.length == 1;
76+
String sample = "explicit";
77+
if (validArgs) {
78+
sample = args[0];
79+
if (!sample.equals("explicit") && !sample.equals("implicit")) {
80+
validArgs = false;
81+
}
82+
}
83+
84+
if (!validArgs) {
85+
System.err.println("Expected auth type argument: implict|explict");
86+
System.exit(1);
87+
}
88+
89+
if (sample.equals("implicit")) {
90+
implicit();
91+
} else {
92+
explicit();
93+
}
94+
}
95+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Copyright 2017, Google, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import org.junit.After;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.junit.runners.JUnit4;
26+
27+
import java.io.ByteArrayOutputStream;
28+
import java.io.PrintStream;
29+
30+
/** Tests for auth samples. */
31+
@RunWith(JUnit4.class)
32+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
33+
public class AuthSnippetsIT {
34+
private ByteArrayOutputStream bout;
35+
private PrintStream out;
36+
37+
@Before
38+
public void setUp() {
39+
bout = new ByteArrayOutputStream();
40+
out = new PrintStream(bout);
41+
System.setOut(out);
42+
}
43+
44+
@After
45+
public void tearDown() {
46+
System.setOut(null);
47+
}
48+
49+
@Test
50+
public void testAuthSnippetsImplicit() throws Exception {
51+
AuthSnippets.main(new String[]{"implicit"});
52+
String got = bout.toString();
53+
assertThat(got).contains("Datasets:");
54+
}
55+
}

0 commit comments

Comments
 (0)