首页 > 解决方案 > 带有 BigQuery 的 Angular10,拒绝访问:BigQuery BigQuery:获取驱动器凭据时权限被拒绝

问题描述

我正在使用“@google-cloud/bigquery”将 Angular10、Firebase 与 BigQuery 集成。我在我的项目中创建了一个函数文件夹,并添加了在 Google Cloud Console 上生成服务帐户 json 后获得的密钥 json 文件。

我的 BigQuery 仪表板中有 2 个数据集,其来源是 Google Drive。但是当我尝试查询这些表时,我遇到了错误。

在我的函数文件夹中,我有 index.js 文件,我在其中使用以下内容初始化我的 BigQuery 客户端:

const bigqueryClient = new BigQuery({
    keyFilename:'./<name of key Json file from servie account>',
    scopes:['https://www.googleapis.com/auth/drive.readonly'],
    projectId:<Project-id>
  });

我收到的错误:“访问被拒绝:BigQuery BigQuery:获取云端硬盘凭据时权限被拒绝。”

此外,如果我在初始化我的大查询客户端时删除了 projectId,控制台上会记录以下错误:

抱歉,没有项目 ID,我们无法连接到云服务\n。您可以使用名为\n "GOOGLE_CLOUD_PROJECT" 的环境变量指定一个

有人能帮我一下吗?

标签: firebasegoogle-bigqueryangular10

解决方案


要解决此问题,您需要在 BigQueryClient 文件中定义您的范围,该文件位于 bigquery 库结构 application/libraries/bigqueryclient/vendor/google/cloud-bigquery/src/BigQueryClient路径的以下路径中。

进行以下更改

    const SCOPE=['https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/bigquery',];
        const INSERT_SCOPE = 'https://www.googleapis.com/auth/bigquery.insertdata';

 

    public function __construct(array $config = [])
        {
            $this->location = $this->pluck('location', $config, false);
            $this->setHttpRetryCodes([502]);
            $this->setHttpRetryMessages([
                'rateLimitExceeded',
                'backendError'
            ]);
            $config += [
                'scopes' => self::SCOPE,
                'projectIdRequired' => true,
                'returnInt64AsObject' => false,
                'restRetryFunction' => $this->getRetryFunction(),
                //@codeCoverageIgnoreStart
                'restCalcDelayFunction' => function ($attempt) {
                    return min(
                        mt_rand(0, 1000000) + (pow(2, $attempt) * 1000000),
                        self::MAX_DELAY_MICROSECONDS
                    );
                }
                //@codeCoverageIgnoreEnd
            ];
            $this->connection = new Rest($this->configureAuthentication($config));
            $this->mapper = new ValueMapper($config['returnInt64AsObject']);
        }

推荐阅读