【问题标题】:Unable to save a query as a view table无法将查询另存为视图表
【发布时间】:2016-04-14 09:43:02
【问题描述】:

我有一个正在运行的查询并且可以看到结果。但是在尝试将查询保存为视图表时,我收到错误消息说

未能保存视图。没有找到适合访问 Google 的凭据 驾驶。请联系表所有者寻求帮助。

我认为问题是由查询中使用的表引起的。该表是从我拥有的谷歌表(带有源 URI)上传的。我试图从项目中启用 Google Drive API,但没有运气。不确定如何授予 BigQuery 访问 Google 云端硬盘的权限。

【问题讨论】:

    标签: google-sheets google-bigquery


    【解决方案1】:

    我怀疑您遇到的问题是 OAuth 范围之一。为了与 Google Drive API 对话以读取数据,您需要使用被授予访问该 API 的凭据。

    如果您使用的是 BigQuery 网页界面并且没有明确授予对云端硬盘的访问权限,则它将无法正常工作。例如,当我第一次尝试“保存到 Google 表格”时,BigQuery UI 会弹出一个 OAuth 提示,要求我授予对 Google Drive 的访问权限。在此之后它可以保存结果。尝试这样做以确保您的凭据具有 Drive 范围,然后再次“保存视图”。

    如果您使用自己的代码来执行此操作,则除了已用于与 BigQuery 对话的 'https://www.googleapis.com/auth/bigquery' 范围之外,您还应请求范围 'https://www.googleapis.com/auth/drive'

    如果您使用的是bq 客户端,它已更新为请求此范围,但您可能需要重新初始化您的身份验证凭据。您可以使用 bq init --delete_credentials 删除凭据,然后您的下一步操作是我们重新请求凭据。

    【讨论】:

    • 在需要驱动器授权的 UI 中运行查询还应提示您使用驱动器范围进行授权。错误下方应该有一个额外的“授权”按钮,指出缺少合适的凭据。
    • 我正在使用 Web UI 来保存视图。虽然我不记得是否已授予对 Google Drive 的访问权限,但我可以将表格保存到 Google Sheets。所以我相信 BigQuery 已经可以访问我的 Google Drive。不确定是否有任何方法可以通过 Web UI 重新初始化我的凭据?
    • 当您创建视图时,BQ 似乎在后台运行查询以检查其有效性。无论它用于测试查询的凭据似乎都缺少对谷歌表格中资源的访问权限。我会说这是一个错误。
    【解决方案2】:

    使用谷歌应用脚​​本这对我有用:

    function saveQueryToTable() {
      var projectId = '...yourprojectid goes here...';
      var datasetId = '...yourdatesetid goes here...';
      var sourceTable = '...your table or view goes here...';
      var destTable = '...destination table goes here...';
    var myQuery;
      
      //just a random call to activate the Drive API scope
      var test = Drive.Properties.list('...drive file id goes here...')
      
      //list all tables for the particular dataset
      var tableList = BigQuery.Tables.list(projectId, datasetId).getTables();
      
      //if the table exist, delete it
      for (var i = 0; i < tableList.length; i++) {
        if (tableList[i].tableReference.tableId == destTable) { 
          BigQuery.Tables.remove(projectId, datasetId, destTable);
          Logger.log("DELETED: " + destTable);
        }
      };
     
     myQuery =  'SELECT * FROM [PROJECTID:DATASETID.TABLEID];'
     .replace('PROJECTID',projectId)
     .replace('DATASETID',datasetId)
     .replace('TABLEID',sourceTable)
      
     var job = {
        configuration: {
          query: {
            query: myQuery,
            destinationTable: {
              projectId: projectId,
              datasetId: datasetId,
              tableId: destTable
            }
          }
        }
      };
    
      var queryResults = BigQuery.Jobs.insert(job, projectId);
      Logger.log(queryResults.status);
    }

    “技巧”是对 Drive API 的随机调用,以确保同时包含 BigQuery 和 Drive 范围。

    Google Apps Script Project Properties

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-06
      • 1970-01-01
      • 2019-07-17
      • 2021-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-25
      相关资源
      最近更新 更多