【问题标题】:how to authenticate `com.google.cloud.bigquery.BigQuery` with service account如何使用服务帐户对“com.google.cloud.bigquery.BigQuery”进行身份验证
【发布时间】:2017-03-20 23:12:09
【问题描述】:

我想使用这个库

com.google.cloud.bigquery.BigQuery

如示例:

https://cloud.google.com/bigquery/create-simple-app-api

但是我想使用服务帐户对BigQuery 客户端进行身份验证。

我该怎么做?

我只看到默认凭据方式。

  BigQuery bigquery =
                new BigQueryOptions.DefaultBigqueryFactory().create(BigQueryOptions.defaultInstance());

【问题讨论】:

    标签: java google-cloud-platform google-api-java-client


    【解决方案1】:

    在构建服务时尝试使用 setCredentials 和 ServiceAccountCredentials.fromStream。

    BigQuery bigquery = BigQueryOptions.newBuilder()
        .setCredentials(
           ServiceAccountCredentials.fromStream(
             new FileInputStream("your_json_service_keyfile.json"))
        )
        .build().getService()
    

    这应该在 Google 的 API 文档中得到更好的记录。

    希望它有所帮助,这是我在 stackoverflow 上的第一个答案 :-)

    【讨论】:

    • 你是队长。这应该被接受的答案
    【解决方案2】:

    请注意,根据Issue #3535,单独setCredentials 可能无法正确设置项目ID(“缺少项目ID”错误),因此应使用setProjectId 明确设置。

    例如

       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();
    

    【讨论】:

    • 感谢您提供问题链接,但该问题已关闭
    • 感谢您的信息。缺乏谷歌文档和过时的样本让我感到非常惊讶和沮丧。 goodle 控制台和 git 中的几乎所有示例都已过时,因为它们尚未使用库的最新更改进行更新..
    猜你喜欢
    • 2020-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 1970-01-01
    • 2017-07-13
    • 1970-01-01
    相关资源
    最近更新 更多