[英]how to authenticate `com.google.cloud.bigquery.BigQuery` with service account
I want to use this library 我想使用这个图书馆
com.google.cloud.bigquery.BigQuery
as in the example: 就像在此样品中:
https://cloud.google.com/bigquery/create-simple-app-api https://cloud.google.com/bigquery/create-simple-app-api
However I want to authenticate the BigQuery
client with service account. 但是,我想使用服务帐户对
BigQuery
客户端进行身份验证。
How can I do this? 我怎样才能做到这一点?
I see only a default credentials way. 我只看到默认的凭据方式。
BigQuery bigquery =
new BigQueryOptions.DefaultBigqueryFactory().create(BigQueryOptions.defaultInstance());
Try using setCredentials and ServiceAccountCredentials.fromStream while building the service. 构建服务时,请尝试使用setCredentials和ServiceAccountCredentials.fromStream。
BigQuery bigquery = BigQueryOptions.newBuilder()
.setCredentials(
ServiceAccountCredentials.fromStream(
new FileInputStream("your_json_service_keyfile.json"))
)
.build().getService()
This ought to be better documented in Google's API doc. 应该在Google的API文档中更好地记录下来。
Hope it helps, it's my first answer on stackoverflow :-) 希望对您有帮助,这是我对stackoverflow的第一个答案:-)
Beware that as per Issue #3535 , the project ID may not be set correctly by setCredentials
alone ("Missing project ID" error) and thus should be set explicitly using setProjectId
. 请注意,根据问题#3535 ,可能无法单独通过
setCredentials
正确设置项目ID(“缺少项目ID”错误),因此应使用setProjectId
进行显式设置。
Eg 例如
ServiceAccountCredentials credentials;
File credentialsPath = new File("/pathToJSONfile/SOME_NAME.json");
try (FileInputStream serviceAccountStream = new FileInputStream(credentialsPath)) {
credentials = ServiceAccountCredentials.fromStream(serviceAccountStream);
}
BigQuery bigquery = BigQueryOptions.newBuilder()
.setCredentials(credentials)
.setProjectId(credentials.getProjectId())
.build().getService();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.