-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthExample.java
More file actions
63 lines (48 loc) · 1.77 KB
/
Copy pathAuthExample.java
File metadata and controls
63 lines (48 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.bigquery.example;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryOptions;
/**
* Examples for authenticating to Google BigQuery.
*
* <p>See: https://cloud.google.com/bigquery/authentication
*/
public class AuthExample {
public static void implicit() {
// Instantiate a client. If you don't specify credentials when constructing a client, the
// client library will look for credentials in the environment, such as the
// GOOGLE_APPLICATION_CREDENTIALS environment variable.
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
}
public static void explicit() throws IOException {
// Load credentials from JSON key file. If you can't set the GOOGLE_APPLICATION_CREDENTIALS
// environment variable, you can explicitly load the credentials file to construct the
// credentials.
GoogleCredentials credentials = null;
File credentialsPath = new File("service_account.json");
FileInputStream serviceAccountStream = new FileInputStream(credentialsPath);
try {
credentials = ServiceAccountCredentials.fromStream(serviceAccountStream);
}catch(Exception e)
{
e.printStackTrace();
}
// Instantiate a client.
BigQuery bigquery =
BigQueryOptions.newBuilder().setCredentials(credentials).
build().getService();
}
// [END explicit_service_account]
public static void main(String args[]) throws IOException {
String sample="implicit";
if (sample.equals("implicit")) {
implicit();
} else {
explicit();
}
}
}