使用服务帐户使用外部数据源访问BigQuery表

时间:2019-02-07 14:29:42

标签: java google-cloud-platform google-bigquery

我如何使用服务帐户作为凭据提供程序来查询基于外部数据源(Google电子表格)的 BigQuery表

我这样初始化BigQuery Java客户端:

GoogleCredentials cred = ServiceAccountCredentials
        .fromStream(credentialsFile.getInputStream());
BigQuery bq = BigQueryOptions.newBuilder()
        .setCredentials(cred).build()
        .getService();

但是,当尝试查询表时,出现身份验证错误

Access Denied: BigQuery BigQuery: No OAuth token with Google Drive scope was found.

在添加驱动器范围时,如下所示:

GoogleCredentials cred = ServiceAccountCredentials
        .fromStream(credentialsFile.getInputStream())
        .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/drive"));

返回Insufficient Permission错误。

1 个答案:

答案 0 :(得分:0)

我也遇到了同样的问题,我按照以下步骤进行了修复

1。在项目中启用Google Drive API

登录到您的Google Cloud Console。从顶部下拉列表中选择您的项目。在左侧导航中,选择“ API和服务”,然后选择“仪表板”。在页面顶部,单击“启用API和服务”。使用搜索栏搜索Google Drive API,选择它,然后单击“启用”。

2。将服务帐户客户ID添加到Google表格

在您的Google Cloud Console中,从左侧栏中选择IAM和管理员,然后选择服务帐户。将值复制到“服务帐户ID”列中。如果您还没有服务帐户,请按照上面的说明连接BigQuery帐户。

打开您的Google表格,然后单击“共享”按钮。单击底部的“高级”,然后在“邀请人员”文本输入中,输入您先前复制的“服务帐户ID”值。

3。添加“云平台”范围

GoogleCredentials cred = ServiceAccountCredentials
            .fromStream(credentialsFile.getInputStream())
            .createScoped(Arrays.asList("https://www.googleapis.com/auth/drive",
                    "https://www.googleapis.com/auth/cloud-platform"));

代码:

        File credentialsFile = new File("credentials.json");
        GoogleCredentials cred = ServiceAccountCredentials
                .fromStream(new FileInputStream(credentialsFile))
                .createScoped(Arrays.asList("https://www.googleapis.com/auth/drive",
                        "https://www.googleapis.com/auth/cloud-platform"));
        BigQuery bigquery = BigQueryOptions.newBuilder()
                .setCredentials(cred).build()
                .getService();

        QueryJobConfiguration queryConfig =
                QueryJobConfiguration.newBuilder(
                        "SELECT * FROM `my_project.dataset.table1`")
                        // Use standard SQL syntax for queries.
                        // See: https://cloud.google.com/bigquery/sql-reference/
                        .setUseLegacySql(false)
                        .build();

        // Create a job ID so that we can safely retry.
        JobId jobId = JobId.of(UUID.randomUUID().toString());
        Job queryJob = bigquery.create(JobInfo.newBuilder(queryConfig).setJobId(jobId).build());

        // Wait for the query to complete.
        queryJob = queryJob.waitFor();

        QueryResponse response = bigquery.getQueryResults(jobId);

        TableResult result = queryJob.getQueryResults();

        // Print all pages of the results.
        for (FieldValueList row : result.iterateAll()) {
            String date = row.get("Date").getStringValue();
            System.out.printf("Date: %s%n", date);
        }

参考:

https://support.chartio.com/docs/data-sources/#google-sheets-via-bigquery