简体   繁体   English

如何使用服务帐户验证com.google.cloud.bigquery.BigQuery`

[英]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.

相关问题 Google Cloud BigQuery Admin服务帐户获得“没有bigquery.jobs.create权限” - Google Cloud BigQuery Admin service account gets “does not have bigquery.jobs.create permission” 使用com.google.api.services.bigquery或com.google.cloud.bigquery库访问用户BigQuery - Access user BigQuery with com.google.api.services.bigquery or com.google.cloud.bigquery library 如何使用服务帐户向Google Cloud SQL Java进行身份验证 - How to use Service Account to authenticate with Google Cloud SQL Java 无法验证服务帐号 - Google Cloud - Unable to authenticate service account - Google Cloud com.google.cloud.bigquery.BigQueryException:读取超时 - com.google.cloud.bigquery.BigQueryException: Read timed out setQueryParameters不是`'com.google.cloud'的一部分,名称:'google-cloud-bigquery',版本:'0.4.0'` - setQueryParameters isn't part of `'com.google.cloud', name: 'google-cloud-bigquery', version: '0.4.0'` 如何在不下载Google Cloud Java SDK的情况下对服务帐户进行身份验证(不在App Engine上) - How to authenticate a service account without download for the Google Cloud Java SDK (not on App Engine) 如何使用 google cloud bigquery 进行集成测试 - How can I make integration tests with google cloud bigquery 使用默认服务帐户的 Bigquery 插入作业 - Bigquery insert job with default service account 如何使用 Google Cloud 中的 Google 服务帐户登录? - How to log in with a Google Service account in the Google Cloud?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM